结合Java反射用简单工厂模式改进抽象工厂模式

前一篇简单介绍了抽象工厂模式,结合Java的反射机制,可以用简单工厂模式来改进抽象工厂模式,减少类的数量,增加可维护性。UML图如下:


去除IFactory、SqlserverFactory和AccessFactory三个工厂类,取而代之的是DataAccess类,用一个简单工厂模式来实现。


代码示例:

1. 两个表User和Department的实体类

public class User {
	private int id;
	private String userName;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public User(int id, String userName) {
		super();
		this.id = id;
		this.userName = userName;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + "]";
	}
	
	
}

public class Department {

	private int id;
	private String deptName;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public Department() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Department(int id, String deptName) {
		super();
		this.id = id;
		this.deptName = deptName;
	}
	@Override
	public String toString() {
		return "Department [deptName=" + deptName + ", id=" + id + "]";
	}
	
	
}


2. 两个表操作的接口

public interface IUserDao {
	
	abstract void insert(User user);
	abstract User select(int id);
	
}

public interface IDepartmentDao {

	abstract void insert(Department dept);
	abstract Department select(int id);
}


3. IUserDao的MySQL实现和Oracle实现

public class UserMysqlImpl implements IUserDao {

	@Override
	public void insert(User user) {
		// TODO Auto-generated method stub
		System.out.println("User: MySQL实现插入一条记录:"+user.toString());
	}

	@Override
	public User select(int id) {
		// TODO Auto-generated method stub
		System.out.println("User: MySQL实现查询 :id = "+id);
		return null;
	}

}
public class UserOracleImpl implements IUserDao {

	@Override
	public void insert(User user) {
		// TODO Auto-generated method stub
		System.out.println("User: Oracle实现插入一条记录:"+user.toString());
	}

	@Override
	public User select(int id) {
		// TODO Auto-generated method stub
		System.out.println("User: Oracle实现查询 :id = "+id);
		return null;
	}

}

4. IDepartmentDao的Mysql实现和oracle实现


public class DepartmentMySqlImpl implements IDepartmentDao {

	@Override
	public void insert(Department dept) {
		// TODO Auto-generated method stub
		System.out.println("Department: MySQL实现插入一条记录." + dept.toString());
	}

	@Override
	public Department select(int id) {
		// TODO Auto-generated method stub
		System.out.println("Department: MySQL查询记录 id = " + id);
		return null;
	}

}

public class DepartmentOracleImpl implements IDepartmentDao{

	@Override
	public void insert(Department dept) {
		// TODO Auto-generated method stub
		System.out.println("Department: Oracle实现插入一条记录." + dept.toString());
	}

	@Override
	public Department select(int id) {
		// TODO Auto-generated method stub
		System.out.println("Department: Oracle实现查询一条记录 id = " + id);
		return null;
	}

}

5.DataAccess类

public class DataAccess {

	private String db;
	
	public IUserDao createUserDao() 
						throws ClassNotFoundException, 
								InstantiationException, 
								IllegalAccessException{
		if(db != null && !db.equals("")){
			//拼接字符串,注意类名包含包名
			String className = "com.design.abstractFactory.product.User" + db +"Impl";
			//获取以className字符串为名的类
			Class<IUserDao> userDaoImplClass 
					= (Class<IUserDao>) Class.forName(className);
			//返回类的实例
			return userDaoImplClass.newInstance();
		}
		return null;
	}
	
	public IDepartmentDao createDepatDao() 
							throws ClassNotFoundException,
									InstantiationException, 
									IllegalAccessException{
		
		if(db != null && !db.equals("")){
			//拼接字符串,注意类名包括包名
			String className = "com.design.abstractFactory.product.Department" + db +"Impl";
			//获取以className字符串为名的类
			Class<IDepartmentDao> deptDaoImplClass 
						= (Class<IDepartmentDao>) Class.forName(className);
			//返回类的实例
			return deptDaoImplClass.newInstance();
		}
		return null;
	}

	public String getDb() {
		return db;
	}

	public void setDb(String db) {
		this.db = db;
	}
	
}

6. client端

public class abstractFactoryTest {
	
	@Test
	public void testDataAccess() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		Properties proper = new Properties();
		InputStream is 
			= this.getClass().getClassLoader().getResourceAsStream("DB.properties");
		proper.load(is);
		String db = proper.getProperty("db");
		
		DataAccess dataAcc = new DataAccess();
		dataAcc.setDb(db);
		
		
		IUserDao userDao =  dataAcc.createUserDao();
		IDepartmentDao deptDao = dataAcc.createDepatDao();
		
		User user = new User(1, "pathfiner");
		Department dept = new Department(1, "aaa");
		
		userDao.insert(user);
		deptDao.select(1);
	}
}

DB.properties

db=MySql


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页