参考:大话设计模式
为了更加清楚的描述这个模式,请先看看下面这个例子。
问题描述
实现两个数的加、减、乘、除,输出结果;
代码
运算工厂 OperationFactory.java
public class OperationFactory {
private String operator;
public OperationFactory(String operator) {
super();
this.operator = operator;
}
public Operation createOperation(double numberA, double numberB) {
Operation operation;
switch (operator) {
case "+":
operation = new OperationAdd(numberA, numberB);
break;
case "-":
operation = new OperationSub(numberA, numberB);
break;
case "*":
operation = new OperationMul(numberA, numberB);
break;
case "/":
operation = new OperationDiv(numberA, numberB);
break;
default:
operation = null;
break;
}
return operation;
}
}
抽象运算类 Operation.java
public abstract class Operation {
private double numberA;
private double numberB;
public Operation(double numberA, double numberB) {
super();
this.numberA = numberA;
this.numberB = numberB;
}
public double getNumberA() {
return numberA;
}
public void setNumberA(double numberA) {
this.numberA = numberA;
}
public double getNumberB() {
return numberB;
}
public void setNumberB(double numberB) {
this.numberB = numberB;
}
public abstract String getResult();
}
加法运算类 OperationAdd.java
public class OperationAdd extends Operation {
public OperationAdd(double numberA, double numberB) {
super(numberA, numberB);
}
@Override
public String getResult() {
return "" + (getNumberA() + getNumberB());
}
}
减法运算类 OperationSub.java
public class OperationSub extends Operation {
public OperationSub(double numberA, double numberB) {
super(numberA, numberB);
}
@Override
public String getResult() {
return "" + (getNumberA() - getNumberB());
}
}
乘法运算类 OperationMul.java
public class OperationMul extends Operation {
public OperationMul(double numberA, double numberB) {
super(numberA, numberB);
}
@Override
public String getResult() {
return "" + (getNumberA() * getNumberB());
}
}
除法运算类 OperationDiv.java
public class OperationDiv extends Operation {
public OperationDiv(double numberA, double numberB) {
super(numberA, numberB);
}
@Override
public String getResult() {
if (getNumberB() == 0) {
System.out.println("除数不能为0!");
return "错误!";
}
return "" + (getNumberA() / getNumberB());
}
}
测试类 Calculator.java
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入数字A:");
double numberA = input.nextDouble();
System.out.println("请选择运算符号(+、-、*、/):");
String operator = input.next();
System.out.println("请输入数字B:");
double numberB = input.nextDouble();
OperationFactory factory = new OperationFactory(operator);
Operation operation = factory.createOperation(numberA, numberB);
if(operation == null){
System.out.println("请输入正确的运算符!");
return;
}
System.out.println("" + numberA + " " + operator + " " + numberB + " = " + operation.getResult());
}
}
输出结果
请输入数字A:
22.22
请选择运算符号(+、-、*、/):
/
请输入数字B:
6
22.22 / 6.0 = 3.703333333333333
简单工厂模式有什么好处?
简单工厂模式使用了面向对象的编程方法实现需求,通过封装、继承、多态把程序的耦合降低,使得程序可维护、可扩展、可复用、灵活性好。
- 把各个运算独立封装起来成为一个类,相互之间互不影响,这里运用到封装;
- 各个运算之间有相同的地方,把相同的地方封装到一个抽象类(
Operation.java
)中,让具体的运算类去继承这个抽象类,这里用到继承; - 在编写测试类的时候,代码
Operation operation = factory.createOperation(numberA, numberB);
用到了多态; -
可维护:当需求变更需要修改加法运算时,只需要修改加法运算类
OperationAdd.java
里的具体实现,对主程序或者其他的运算也没有任何影响; -
可扩展:当需要添加一个开根运算时,只需要添加一个实现类,让其继承自抽象类(
Operation.java
),再在其中添加具体算法,对主程序或者其他的运算也没有任何影响; - 可复用: 由于每个运算都是一个独立的类实现,所以有很好的利用性;
- 灵活性好:上面说了那么多,可以看出来灵活性很好;
什么时候使用简单工厂模式?
当我们明确的计划不同条件下创建不同实例时,使用它
网友评论