题目:实现最基本的数据访问程序,以“新增用户”和得到用户为例。假设只有ID和Name字段,其余省略。
第一版,最简单的实现:
public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public class SqlserverUser { public void Insert(User user){ System.out.println("在sqlserver中给user表添加了一条数据"); } public User getUser(int id){ System.out.println("在sqlserver中根据id得到user表中的一条数据"); return null; } }
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub User user = new User(); SqlserverUser su = new SqlserverUser(); su.Insert(user); su.getUser(1); } }
代码是简单,但这里不够灵活,被框死在Sqlserver上了,如果要用Access呢?再者被操作的数据也不只有User,还有可能有Department等其他对象。
第二版,抽象工厂模式实现数据访问程序:
代码结构图
这里比较复杂点,实现的是多对的的功能,多个数据库对多个数据表的操作,在这里会出现2x2=4种情况。
public class Department { }
public interface IFactory { IUser createUser(); IDepartment createDepartment(); }
public class SqlserverFactory implements IFactory{ @Override public IUser createUser() { return new SqlserverUser1(); } @Override public IDepartment createDepartment() { // TODO Auto-generated method stub return new SqlserverDepartment1(); } }
public class AccessFactory implements IFactory{ @Override public IUser createUser() { return new AccessUser1(); } @Override public IDepartment createDepartment() { // TODO Auto-generated method stub return new AccessDepartment1(); } }
public interface IUser { void Insert(User user); User getUser(int id); }
public interface IDepartment { void insert(Department department); Department getDepartment(int id); }
public class SqlserverUser1 implements IUser{ public void Insert(User user){ System.out.println("在sqlserver中给user表添加了一条数据"); } public User getUser(int id){ System.out.println("在sqlserver中根据id得到user表中的一条数据"); return null; } }
public class AccessUser1 implements IUser{ public void Insert(User user){ System.out.println("在Access中给user表添加了一条数据"); } public User getUser(int id){ System.out.println("在Access中根据id得到user表中的一条数据"); return null; } }
public class SqlserverDepartment1 implements IDepartment{ public void insert(Department department){ System.out.println("在sqlserver中给department表添加了一条数据"); } public Department getDepartment(int id){ System.out.println("在sqlserver中根据id得到department表中的一条数据"); return null; } }
public class AccessDepartment1 implements IDepartment{ public void insert(Department department){ System.out.println("在Access中给department表添加了一条数据"); } public Department getDepartment(int id){ System.out.println("在Access中根据id得到department表中的一条数据"); return null; } }
public class Test1 { public static void main(String[] args) { User user = new User(); Department department = new Department(); IFactory factory = new SqlserverFactory(); IUser iu = factory.createUser(); iu.Insert(user); iu.getUser(1); IDepartment id = factory.createDepartment(); id.insert(department); id.getDepartment(1); } }
这就做到了只需更改 IFactory factory = new SqlserverFactory() 为 IFactory factory = new AccessFactory()就实现了数据库的访问切换。只有一个user和user操作类的时候,我们只需要用工厂方法模式,但现在数据中有很多个表,而Sqlserver和Access又是两大不同的分类,所以解决这种涉及多个产品系列的问题,就用抽象工厂模式。
抽象工厂模式: 提供一个创建一系列相关或互相依赖对象的接口,而无需指定他们具体的类。