抽象工厂模式(AbstractFactory):提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类。
UML图:
IFactory:
package com.cnblog.clarck; public interface IFactory { IUser createUser(); IDepartment createDepartment(); }
SqlserverFactory:
package com.cnblog.clarck; public class SqlserverFactory implements IFactory { @Override public IUser createUser() { return new SqlserverUser(); } @Override public IDepartment createDepartment() { return new SqlserverDepartment(); } }
AccessFactory:
package com.cnblog.clarck; public class AccessFactory implements IFactory { @Override public IUser createUser() { return new AccessUser(); } @Override public IDepartment createDepartment() { return new AccessDepartment(); } }
IUser:
package com.cnblog.clarck; public interface IUser { void insert(User user); User getUser(int id); }
AccessUser:
package com.cnblog.clarck; public class AccessUser implements IUser { @Override public void insert(User user) { System.out.println("在Access中给User表增加一条记录"); } @Override public User getUser(int id) { System.out.println("在Access中根据ID得到User表一条记录"); return null; } }
SqlserverUser:
package com.cnblog.clarck; public class SqlserverUser implements IUser { @Override public void insert(User user) { System.out.println("在SQL Server中给User表增加一条记录"); } @Override public User getUser(int id) { System.out.println("在SQL Server中根据ID得到User表一条记录"); return null; } }
IDepartment:
package com.cnblog.clarck; /** * IDepartment 接口,用于客户端访问,解除与具体数据库访问的耦合 * * @author clarck * */ public interface IDepartment { public void insert(Department department); public Department getDepartment(int id); }
AccessDepartment:
package com.cnblog.clarck; public class AccessDepartment implements IDepartment { @Override public void insert(Department department) { System.out.println("在Access中给Department表增加一条记录"); } @Override public Department getDepartment(int id) { System.out.println("在Access中根据ID得到Department表一条记录"); return null; } }
SqlserverDepartment:
package com.cnblog.clarck; public class SqlserverDepartment implements IDepartment { @Override public void insert(Department department) { System.out.println("在SQL Server中给Department表添加一条记录"); } @Override public Department getDepartment(int id) { System.out.println("在SQL Server中根据ID得到Department表的一条记录"); return null; } }
Department:
package com.cnblog.clarck; public class Department { private int mID; private String mDeptName; public int getID() { return mID; } public void setID(int mID) { this.mID = mID; } public String getDeptName() { return mDeptName; } public void setDeptName(String mDeptName) { this.mDeptName = mDeptName; } }
User:
package com.cnblog.clarck; public class User { private int mID; private String mName; public int getID() { return mID; } public void setID(int mID) { this.mID = mID; } public String getName() { return mName; } public void setName(String mName) { this.mName = mName; } }
Client:
package com.cnblog.clarck; public class Client { public static void main(String[] args) { User user = new User(); Department department = new Department(); //IFactory factory = new SqlserverFactory(); IFactory factory = new AccessFactory(); IUser iu = factory.createUser(); iu.insert(user); iu.getUser(1); IDepartment iDepartment = factory.createDepartment(); iDepartment.insert(department); iDepartment.getDepartment(1); } }