标签:back blog 大于 style sql数据库 factory ring 分享 数据
抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖的接口,而无需指定它们具体的类。
涉及到多个产品系列的问题,工厂方法模式组合叫抽象工厂模式。如下图涉及到大于1个的产品,dept、user两个产品
只是涉及到一个产品系列问题,才叫工厂方法模式。如下图 只涉及Leifeng这一产品
抽象工厂模式代码展示开始
Department类
package abstractFactory.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 abstractFactory.dept; public interface IDeptDao { void insert(Department dept); void getDept(int id); }
MysqlDeptDao类
package abstractFactory.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 abstractFactory.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获取一条记录"); } }
User类
package abstractFactory.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; } }
IUserDao类
package abstractFactory.user; public interface IUserDao { void insert(User user); void getUser(int id); }
MysqlUserDao类
package abstractFactory.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 abstractFactory.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获取一条记录"); } }
IFactory类
package abstractFactory; import abstractFactory.dept.IDeptDao; import abstractFactory.user.IUserDao; public interface IFactory { IUserDao createUser(); IDeptDao createDept(); }
MysqlFactory类
package abstractFactory; import abstractFactory.dept.IDeptDao; import abstractFactory.dept.MysqlDeptDao; import abstractFactory.user.IUserDao; import abstractFactory.user.MysqlUserDao; public class MysqlFactory implements IFactory { @Override public IUserDao createUser() { return new MysqlUserDao(); } @Override public IDeptDao createDept() { return new MysqlDeptDao(); } }
OracleFactory类
package abstractFactory; import abstractFactory.dept.IDeptDao; import abstractFactory.dept.OracleDeptDao; import abstractFactory.user.IUserDao; import abstractFactory.user.OracleUserDao; public class OracleFactory implements IFactory { @Override public IDeptDao createDept() { return new OracleDeptDao(); } @Override public IUserDao createUser() { return new OracleUserDao(); } }
Business类
package abstractFactory; import abstractFactory.dept.Department; import abstractFactory.dept.IDeptDao; import abstractFactory.user.IUserDao; import abstractFactory.user.User; public class Business { public static void main(String[] args) { //产品1系列 User user = new User(); IFactory factory = new OracleFactory(); //IFactory factory = new MysqlFactory(); IUserDao iuDao = factory.createUser(); iuDao.insert(user); iuDao.getUser(1); //产品2系列 Department dept = new Department(); IDeptDao idepatDao = factory.createDept(); idepatDao.insert(dept); idepatDao.getDept(1); } }
package abstractFactory.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;}}
标签:back blog 大于 style sql数据库 factory ring 分享 数据
原文地址:http://www.cnblogs.com/ccgjava/p/7092494.html