05-工厂模式与简单工厂模式
05-工厂模式与简单工厂模式
简单工厂模式
计算器实现
package com.gof.simpleFactory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public abstract class Operation {
private double num1;
private double num2;
public Operation() {
}
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
public abstract void getResult();
}
package com.gof.simpleFactory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class AddOperation extends Operation {
@Override
public void getResult() {
System.out.println(getNum1() + getNum2());
}
}
package com.gof.simpleFactory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class SubtractOperation extends Operation {
@Override
public void getResult() {
System.out.println(getNum1() - getNum2());
}
}
package com.gof.simpleFactory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class MultiplyOperation extends Operation {
@Override
public void getResult() {
System.out.println(getNum1() * getNum2());
}
}
package com.gof.simpleFactory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class DivideOperation extends Operation {
@Override
public void getResult() {
if (getNum2() == 0) {
System.out.println("wrong num2");
} else {
System.out.println(getNum1() / getNum2());
}
}
}
简单工厂
package com.gof.simpleFactory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class OpeationFactory {
private static Operation operation;
public static Operation operate(String operator) {
switch (operator) {
case "+":
operation = new AddOperation();
break;
case "-":
operation = new SubtractOperation();
break;
case "*":
operation = new MultiplyOperation();
break;
case "/":
operation = new DivideOperation();
break;
}
return operation;
}
}
客户端
package com.gof.simpleFactory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class Client {
public static void main(String[] args) {
Operation operate = OpeationFactory.operate("-");
operate.setNum1(234);
operate.setNum2(1);
operate.getResult();
}
}
工厂模式
工厂模式相较与简单工厂模式,仅针对简单工厂的代码及客户端的代码进行改进
一个工厂接口和4个工厂类
package com.gof.factory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public interface OperationFactory {
Operation createOperation();
}
package com.gof.factory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class AddFactory implements OperationFactory {
@Override
public Operation createOperation() {
return new AddOperation();
}
}
package com.gof.factory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class SubtractFactory implements OperationFactory {
@Override
public Operation createOperation() {
return new SubtractOperation();
}
}
package com.gof.factory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class MultiplyFactory implements OperationFactory {
@Override
public Operation createOperation() {
return new MultiplyOperation();
}
}
package com.gof.factory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class DivideFactory implements OperationFactory {
@Override
public Operation createOperation() {
return new DivideOperation();
}
}
客户端
package com.gof.factory;
/**
* 功能描述
*
* @since 2023-01-18
*/
public class Client {
public static void main(String[] args) {
AddFactory addFactory = new AddFactory();
Operation operation = addFactory.createOperation();
operation.setNum1(234);
operation.setNum2(1);
operation.getResult();
}
}
比较
简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖
工厂模式的优势在于遵循了开放封闭原则,对扩展开放,对修改封闭