标签:tee 配置文件 相关 clipboard oracle数据库 初始化 get art sys
package designpattern.abstractfactory; public abstract class Employee { private String name; abstract void insert(Employee employee); public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee [name=" + name + "]"; } }
package designpattern.abstractfactory; public class OracleEmployee extends Employee { @Override void insert(Employee employee) { System.out.println("往oracle数据库插入一条Employee员工数据:" + employee); } }
package designpattern.abstractfactory; public class MysqlEmployee extends Employee { @Override public void insert(Employee employee) { System.out.println("往mysql数据库插入一条Employee员工数据:" + employee); } }
package designpattern.abstractfactory; public abstract class Department { String name; abstract void insert(Department department); public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Department [name=" + name + "]"; } }
package designpattern.abstractfactory; public class OracleDepartment extends Department { @Override void insert(Department department) { System.out.println("往oracle数据库插入一条Department部门数据:" + department); } }
package designpattern.abstractfactory; public class MysqlDepartment extends Department { @Override void insert(Department department) { System.out.println("往mysql数据库插入一条Department部门数据:"+department); } }
package designpattern.abstractfactory; public interface Factory { Employee createEmployee(); Department createDepartment(); }
package designpattern.abstractfactory; public class MysqlFactory implements Factory { @Override public Employee createEmployee() { return new MysqlEmployee(); } @Override public Department createDepartment() { return new MysqlDepartment(); } }
package designpattern.abstractfactory; public class OracleFactory implements Factory { @Override public Employee createEmployee() { return new OracleEmployee(); } @Override public Department createDepartment() { return new OracleDepartment(); } }
package designpattern.abstractfactory; public class Client { public static void main(String[] args) { Factory factory = new MysqlFactory(); // Factory factory=new OracleFactory(); Employee employee = factory.createEmployee(); employee.setName("张三"); employee.insert(employee); Department department = factory.createDepartment(); department.setName("技术部"); department.insert(department); } }
往mysql数据库插入一条Employee员工数据:Employee [name=张三]
往mysql数据库插入一条Department部门数据:Department [name=技术部]
package designpattern.abstractfactory; public class SimpleFactory { static String db = "mysql"; //static String db="oracle"; static Employee createEmployee() { switch (db) { case "mysql": return new MysqlEmployee(); case "oracle": return new OracleEmployee(); default: return null; } } static Department createDepartment() { switch (db) { case "mysql": return new MysqlDepartment(); case "oracle": return new OracleDepartment(); default: return null; } } }
package designpattern.abstractfactory; public class Client2 { public static void main(String[] args) { Employee employee = SimpleFactory.createEmployee(); employee.setName("张三"); employee.insert(employee); Department department = SimpleFactory.createDepartment(); department.setName("技术部"); department.insert(department); } }
package designpattern.abstractfactory; public class ReflectSimpleFactory { static String db = "Mysql"; // static String db="Oracle"; static String path = "designpattern.abstractfactory";// 包路径 static Employee createEmployee() { try { Class<Employee> employee = (Class<Employee>) Class.forName(path + "." + db + "Employee"); return employee.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } static Department createDepartment() { try { Class<Department> department = (Class<Department>) Class.forName(path + "." + db + "Department"); return department.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } }
package designpattern.abstractfactory; public class Client3 { public static void main(String[] args) { Employee employee = ReflectSimpleFactory.createEmployee(); employee.setName("张三"); employee.insert(employee); Department department = ReflectSimpleFactory.createDepartment(); department.setName("技术部"); department.insert(department); } }
package designpattern.abstractfactory; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReflectSimpleFactory2 { static String path = "designpattern.abstractfactory";// 包路径 static Employee createEmployee() { try { Class<Employee> employee = (Class<Employee>) Class.forName(path + "." + getDBName() + "Employee"); return employee.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } static Department createDepartment() { try { Class<Department> department = (Class<Department>) Class.forName(path + "." + getDBName() + "Department"); return department.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } private static String getDBName() { String dbName = null; try { InputStream in = ReflectSimpleFactory2.class.getResourceAsStream("db.properties"); Properties pro = new Properties(); pro.load(in); in.close(); dbName = pro.getProperty("db"); } catch (IOException e) { e.printStackTrace(); } return dbName; } }
db=Mysql
#db=Oracle
package designpattern.abstractfactory; public class Client4 { public static void main(String[] args) { Employee employee = ReflectSimpleFactory2.createEmployee(); employee.setName("张三"); employee.insert(employee); Department department = ReflectSimpleFactory2.createDepartment(); department.setName("技术部"); department.insert(department); } }
设计模式 | 抽象工厂模式(abstract factory)
标签:tee 配置文件 相关 clipboard oracle数据库 初始化 get art sys
原文地址:https://www.cnblogs.com/imoqian/p/10777422.html