返回信息流这个模式好像比较常用噢。说说你的使用情况呗
这是一条镜像帖。来源:北邮人论坛 / java / #36785同步于 2014/12/5
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
大家开发用工厂模式了吗?
studychina
2014/12/5镜像同步9 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
import javax.sql.DataSource;
class MyDbInterface {
private DataSource dataSource;
// insert getters and setters here
public void doQuery() {
try(Connection conn = dataSource.getConnection()) { // create a connection
// do query here.
}
}
}
这个DataSource其实就是工厂。可以配置成具体数据库的工厂类。
import org.mariadb.jdbc.MySQLDataSource;
import org.postgresql.ds.PGPoolingDataSource;
import oracle.jdbc.pool.OracleDataSource;
public static void main(String[] args) {
MyDbInterface mdi = new MyDbInterface();
// if use MariaDB
MysqlDataSource ds = new MysqlDataSource();
ds.setUser("foo");
ds.setPassword("bar");
ds.setDatabaseName("baz");
mdi.setDataSource(ds);
// if use PostgreSql
PGPoolingDataSource ds = new PGPoolingDataSource()
ds.setUser("foo");
ds.setPassword("bar");
ds.setDatabaseName("baz");
mdi.setDataSource(ds);
// if use oracle
OracleDataSource ds = new OracleDataSource();
ds.setUser("foo");
ds.setPassword("bar");
ds.setDatabaseName("baz");
mdi.setDataSource(ds);
mdi.doQuery();
}