package gof23; public class AbstractFactoryTest { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { //操作SqlServer数据库 DataAccess.setFullyQualifiedName("gof23.Sqlserver"); IUser user = DataAccess.createUser(); user.insert(null); user.getUser(0); IDepartment dept = DataAccess.createDepartment(); dept.insert(null); dept.getDept(0); System.out.println("\n"+"---------------我是华丽的分割线------------------"+"\n"); //操作Mysql数据库 DataAccess.setFullyQualifiedName("gof23.Mysql"); user = DataAccess.createUser(); user.insert(null); user.getUser(0); dept = DataAccess.createDepartment(); dept.insert(null); dept.getDept(0); } } /* * 用户表的字段 */ 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; } } /* * 部门表的字段 */ 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; } } /* * 利用反射技术,去除switch或if,解除分支判断带来的耦合 */ class DataAccess { private static String FullyQualifiedName; public static String getFullyQualifiedName() { return FullyQualifiedName; } public static void setFullyQualifiedName(String fullyQualifiedName) { FullyQualifiedName = fullyQualifiedName; } public static IUser createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException { String name = FullyQualifiedName + "User"; return (IUser) Class.forName(name).newInstance(); } public static IDepartment createDepartment() throws InstantiationException, IllegalAccessException, ClassNotFoundException { String name = FullyQualifiedName + "Department"; return (IDepartment) Class.forName(name).newInstance(); } } /* * 用户表的接口 */ interface IUser{ public void insert(User user); public User getUser(int id); } /* * Sqlserver用户表的实现 */ 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; } } /* * Mysql用户表的实现 */ class MysqlUser implements IUser { @Override public void insert(User user) { System.out.println("在Mysql中给User表增加一条记录"); } @Override public User getUser(int id) { System.out.println("在Mysql中根据ID得到User表一条记录"); return null; } } /* * 部门表的接口 */ interface IDepartment { public void insert(Department dep); public Department getDept(int id); } /* * Sqlserver部门表的实现 */ class SqlserverDepartment implements IDepartment { @Override public void insert(Department dep) { System.out.println("在SQL server中给Department表增加一条记录"); } @Override public Department getDept(int id) { System.out.println("在SQL server中根据ID得到Department表一条记录"); return null; } } /* * Mysql部门表的实现 */ class MysqlDepartment implements IDepartment { @Override public void insert(Department dep) { System.out.println("在Mysql中给Department表增加一条记录"); } @Override public Department getDept(int id) { System.out.println("在Mysql中根据ID得到Department表一条记录"); return null; } }
在SQL server中给User表增加一条记录 在SQL server中根据ID得到User表一条记录 在SQL server中给Department表增加一条记录 在SQL server中根据ID得到Department表一条记录 ---------------我是华丽的分割线------------------ 在Mysql中给User表增加一条记录 在Mysql中根据ID得到User表一条记录 在Mysql中给Department表增加一条记录 在Mysql中根据ID得到Department表一条记录
原文:http://blog.csdn.net/hjiam2/article/details/38142691