简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
抽象产品角色是所有产品的父类,提供产品的公共行为
具体产品角色是创建目标,所有创建的对象的实例
工厂角色负责创建所有对象
通过简单工厂模式实现一个计算器的程序,需要完成两个数字的加减乘除操作
计算器设计类图:
实现代码:
design_mode/src/main/java/com/nongminggong/factory/simple at liuyu · constantRAIN/design_mode
客户端调用代码
public class OperationClient {
public static void main(String[] args) {
OperationNumber operationNumber = new OperationNumber(10, 5);
// 加法
Operation operationAdd = OperationFactory.createOperation("+");
double addResult = operationAdd.operate(operationNumber);
System.out.println("加法结果" + addResult);
// 减法
Operation operationSub = OperationFactory.createOperation("-");
double subResult = operationSub.operate(operationNumber);
System.out.println("减法结果" + subResult);
// 乘法
Operation operationMul = OperationFactory.createOperation("*");
double mulResult = operationMul.operate(operationNumber);
System.out.println("乘法结果" + mulResult);
// 除法
Operation operationDiv = OperationFactory.createOperation("/");
double divResult = operationDiv.operate(operationNumber);
System.out.println("除法结果" + divResult);
}
}
执行结果:
优点:
缺点:
工厂方法模式(Factory Methon Pattern) 又称 工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Polymorphic Factory)模式, 它属于创建型模式。在工厂方法模式中,工厂父类负责定义创建产品对象的工接口,而工厂子类则负责生成具体的产品对象,这样做的目的是将产品类的实例化操作延迟到工厂子类中完成,即通过工厂子类来确定究竟应该实例化哪一个具体产品
通过工厂方法模式实现一个计算器的程序,需要完成两个数字的加减乘除操作
计算器设计类图:
实现代码:
design_mode/src/main/java/com/nongminggong/factory/method at liuyu · constantRAIN/design_mode
客服端调用代码:
public class OperationClient {
public static void main(String[] args) {
OperationNumber operationNumber = new OperationNumber(10, 5);
// 加法
OperationFactory operationAddFactory = new OperationAddFactory();
Operation operationAdd = operationAddFactory.createOperation();
double addResult = operationAdd.operate(operationNumber);
System.out.println("加法结果" + addResult);
// 减法
OperationFactory operationSubFactory = new OperationSubFactory();
Operation operationSub = operationSubFactory.createOperation();
double subResult = operationSub.operate(operationNumber);
System.out.println("减法结果" + subResult);
// 乘法
OperationFactory operationMulFactory = new OperationMulFactory();
Operation operationMul = operationMulFactory.createOperation();
double mulResult = operationMul.operate(operationNumber);
System.out.println("乘法结果" + mulResult);
// 除法
OperationFactory operationDivFactory = new OperationDivFactory();
Operation operationDiv = operationDivFactory.createOperation();
double divResult = operationDiv.operate(operationNumber);
System.out.println("除法结果" + divResult);
}
}
执行结果:
优点:
缺点:
工厂方法模式是简单工厂的一种推广和抽象,提高了工厂的扩展性
抽象工厂模式(Abstract Factory Pattern): 提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式
Factory: 抽象工厂
ConcreteFactory:具体工厂
Product和Product2:抽象产品
ConcreteProdcut1和ConcreteProduct2:具体产品
通过sqlServer和Access完成创建用户、查询用户和创建员工、查询员工的功能
场景类图:
实现代码:
design_mode/src/main/java/com/nongminggong/factory/abstracts at liuyu · constantRAIN/design_mode
客户端代码:
public class OperationClient {
public static void main(String[] args) {
AbstractFactory sqlServerFactory = new SqlServerFactory();
UserService sqlServerUser = sqlServerFactory.createUser();
sqlServerUser.insert(new User());
sqlServerUser.getUser();
DepartmentService sqlServerDepartment = sqlServerFactory.createDepartment();
sqlServerDepartment.insert(new Department());
sqlServerDepartment.getDepartment();
AbstractFactory accessFactory = new AccessFactory();
UserService accessUser = accessFactory.createUser();
accessUser.insert(new User());
accessUser.getUser();
DepartmentService accessDepartment = accessFactory.createDepartment();
accessDepartment.insert(new Department());
accessDepartment.getDepartment();
}
}
执行结果:
优点:
缺点: