1、简单工厂模式
- 例:让我们写一个加减乘除的方法,最初的写法:(为了方便未判定除法时分母为
0
的状况)
public class Operation {
public static double getReault(double left ,double right,String op) {
double result = 0;
switch (op) {
case "+":
result = left+right;
break;
case "-":
result = left-right;
break;
case "*":
result = left*right;
break;
case "/":
result = left/right;
break;
default:
break;
}
return result;
}
}
这种方法虽然在一定程度上将功能进行抽离,但是对于增加运算不是很有利。
- 当我们运用继承特性时,例:
//首先定义一个父类
public class Operation {
private double left;
private double right;
public double getLeft() {
return left;
}
public void setLeft(double left) {
this.left = left;
}
public double getRight() {
return right;
}
public void setRight(double right) {
this.right = right;
}
public double getReault() {
return 0;
}
}
//第二步,创建加减乘除四个类继承Operation
public class OperationAdd extends Operation {
@Override
public double getReault() {
return this.getLeft() + this.getRight();
}
}
public class OperationSub extends Operation {
@Override
public double getReault() {
return this.getLeft() - this.getRight();
}
}
public class OperationMul extends Operation {
@Override
public double getReault() {
return this.getLeft() * this.getRight();
}
}
public class OperationDiv extends Operation {
@Override
public double getReault() {
return this.getLeft() / this.getRight();
}
}
//定义工厂类
public class OperationFactory {
public static Operation craeteOperation(String op) {
Operation operation = null;
switch (op) {
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
default:
break;
}
return operation;
}
}
//主函数测试类
public class Test {
public static void main(String[] args) {
Operation operation = OperationFactory.craeteOperation("+");
operation.setLeft(1);
operation.setRight(2);
System.out.println(operation.getReault());
}
}
这时我们可以根据输入的符号通过工厂动态创建对象完成加减乘除运算;
如果出现新的需求,例如增加sqrt
函数,只需要创建一个类继承Operation
,乍一看代码量好像增加了,但是代码的扩展性提高了。
- 既然可以通过继承类实现简单工厂模式,那么接口也可以做到,在本例中接口可能不如继承类代码量更少。
//首先定义一个接口
public interface Operation {
//将公共方法抽出定义在接口中
double getResult(double left,double right);
}
//加
public class OperationAdd implements Operation {
@Override
public double getResult(double left, double right) {
return left + right;
}
}
//减
public class OperationSub implements Operation {
@Override
public double getResult(double left, double right) {
return left - right;
}
}
//乘
public class OperationMul implements Operation {
@Override
public double getResult(double left, double right) {
return left * right;
}
}
//除
public class OperationDiv implements Operation {
@Override
public double getResult(double left, double right) {
return left / right;
}
}
//定义的工厂类没有什么变化
public class OperationFactory {
public static Operation craeteOperation(String op) {
Operation operation = null;
switch (op) {
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
default:
break;
}
return operation;
}
}
//主函数测试类
public class Test {
public static void main(String[] args) {
Operation operation = OperationFactory.craeteOperation("+");
System.out.println(operation.getResult(1, 3));
}
}
参考资料:大话设计模式,程杰著