反射+配置文件+抽象工厂
用反射技术去除Switch或者if,解除简单工厂类的判断语句带来的耦合。
用配置文件全项目替代数据库类型实现Oracle、Mysql,或者加入SqlServer等数据库实现。
优点:比抽象工厂减少耦合(Ifactory,MysqlFactory,OracleFactory变为一个ReflectSqlFactory,而且切换数据库只需要改配置文件)
比简单工厂减少耦合(采用反射技术,减少switch或if 语句)
sql.properties
db=Mysql
ReflectSqlFactory类
package reflectAbstractFactory; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.util.Iterator; import java.util.Properties; import reflectAbstractFactory.dept.IDeptDao; import reflectAbstractFactory.user.IUserDao; public class ReflectSqlFactory { public String getDb() { Properties prop = new Properties(); String db = ""; try { // 读取属性文件a.properties InputStream in = new BufferedInputStream(new FileInputStream( "E:/workspace/dahuasheji/sampleFactory/src/reflectAbstractFactory/sql.properties")); prop.load(in); // /加载属性列表 Iterator<String> it = prop.stringPropertyNames().iterator(); while (it.hasNext()) { String key = it.next(); if("db".endsWith(key)){ db = prop.getProperty("db"); break; } } in.close(); } catch (Exception e) { System.out.println(e); } return db; } public Class<?> getsqlFactory(String pge,String type) throws ClassNotFoundException{ String className= "reflectAbstractFactory."+pge+getDb()+type+"Dao"; System.out.println(className); Class<?> c1 = Class.forName(className); System.out.println(c1.getName()); return c1; } public IUserDao createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException{ Class<?> c = getsqlFactory("user.","User"); IUserDao iu = (IUserDao)c.newInstance(); return iu; }; public IDeptDao createDept() throws ClassNotFoundException, InstantiationException, IllegalAccessException{ Class<?> c = getsqlFactory("dept.","Dept"); IDeptDao id = (IDeptDao)c.newInstance(); return id; }; }
Business类
package reflectAbstractFactory; import reflectAbstractFactory.dept.Department; import reflectAbstractFactory.dept.IDeptDao; import reflectAbstractFactory.user.IUserDao; import reflectAbstractFactory.user.User; public class Business { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { //产品1系列 User user = new User(); ReflectSqlFactory reflectSqlFactory = new ReflectSqlFactory(); IUserDao iuDao = reflectSqlFactory.createUser(); iuDao.insert(user); iuDao.getUser(1); //产品2系列 Department dept = new Department(); IDeptDao idepatDao = reflectSqlFactory.createDept(); idepatDao.insert(dept); idepatDao.getDept(1); } }
Department类
package reflectAbstractFactory.dept; public class Department { private long id; private String deptName; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } }
IDeptDao类
package reflectAbstractFactory.dept; public interface IDeptDao { void insert(Department dept); void getDept(int id); }
MysqlDeptDao类
package reflectAbstractFactory.dept; public class MysqlDeptDao implements IDeptDao { @Override public void insert(Department dept) { System.out.println("在mysql数据库的department表插入一条记录"); } @Override public void getDept(int id) { System.out.println("在mysql数据库的department表根据ID获取一条记录"); } }
OracleDeptDao类
package reflectAbstractFactory.dept; public class OracleDeptDao implements IDeptDao { @Override public void insert(Department dept) { System.out.println("在oracle数据库的department表插入一条记录"); } @Override public void getDept(int id) { System.out.println("在oracle数据库的department表根据ID获取一条记录"); } }
IUserDao类
package reflectAbstractFactory.user; public interface IUserDao { void insert(User user); void getUser(int id); }
MysqlUserDao类
package reflectAbstractFactory.user; public class MysqlUserDao implements IUserDao { @Override public void insert(User user) { System.out.println("在mysql数据库的user表插入一条记录"); } @Override public void getUser(int id) { System.out.println("在mysql数据库的user表根据ID获取一条记录"); } }
OracleUserDao类
package reflectAbstractFactory.user; public class OracleUserDao implements IUserDao { @Override public void insert(User user) { System.out.println("在oracle数据库的user表插入一条记录"); } @Override public void getUser(int id) { System.out.println("在oracle数据库的user表根据ID获取一条记录"); } }
User类
package reflectAbstractFactory.user; public class User { private long id; private String name; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package reflectAbstractFactory;
import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util.Properties;
import reflectAbstractFactory.dept.IDeptDao;import reflectAbstractFactory.user.IUserDao;
public class ReflectSqlFactory {public String getDb() {Properties prop = new Properties();String db = "";try {// 读取属性文件a.propertiesInputStream in = new BufferedInputStream(new FileInputStream("E:/workspace/dahuasheji/sampleFactory/src/reflectAbstractFactory/sql.properties"));prop.load(in); // /加载属性列表Iterator<String> it = prop.stringPropertyNames().iterator();while (it.hasNext()) {String key = it.next();if("db".endsWith(key)){db = prop.getProperty("db");break;}}in.close();} catch (Exception e) {System.out.println(e);}return db;}public Class<?> getsqlFactory(String pge,String type) throws ClassNotFoundException{String className= "reflectAbstractFactory."+pge+getDb()+type+"Dao";System.out.println(className);Class<?> c1 = Class.forName(className);System.out.println(c1.getName());return c1;}public IUserDao createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException{Class<?> c = getsqlFactory("user.","User");IUserDao iu = (IUserDao)c.newInstance();return iu;};public IDeptDao createDept() throws ClassNotFoundException, InstantiationException, IllegalAccessException{Class<?> c = getsqlFactory("dept.","Dept");IDeptDao id = (IDeptDao)c.newInstance();return id;};}