>>>大话设计模式读书笔记系列目录>>>
简单工厂模式(静态工厂模式)
简单设计模式不符合开放-封闭原则,如果新增一种运算需要涉及改动的类过多。
场景:采用简单工厂模式设计一款运算器可以自由添加运算规则
计算器运算抽象类
1 /** 2 * 计算器运算抽象类 3 */ 4 public abstract class Operation { 5 /** 6 * 操作数A 7 */ 8 private double numberA; 9 /** 10 * 操作数B 11 */ 12 private double numberB; 13 14 /** 15 * 抽象方法 16 * 获得操作结果 17 * 18 * @return 19 */ 20 public abstract double getResult(); 21 22 /** 23 * 获取 操作数A 24 * 25 * @return numberA 操作数A 26 */ 27 public double getNumberA() { 28 return this.numberA; 29 } 30 31 /** 32 * 设置 操作数A 33 * 34 * @param numberA 操作数A 35 */ 36 public void setNumberA(double numberA) { 37 this.numberA = numberA; 38 } 39 40 /** 41 * 获取 操作数B 42 * 43 * @return numberB 操作数B 44 */ 45 public double getNumberB() { 46 return this.numberB; 47 } 48 49 /** 50 * 设置 操作数B 51 * 52 * @param numberB 操作数B 53 */ 54 public void setNumberB(double numberB) { 55 this.numberB = numberB; 56 } 57 58 @Override 59 public String toString() { 60 return "Operation{" + 61 "numberA=" + numberA + 62 ", numberB=" + numberB + 63 '}'; 64 } 65 }
运算器子类
1 /** 2 * 加法运算类 3 */ 4 public class OperationAdd extends Operation { 5 6 @Override 7 public double getResult() { 8 return super.getNumberA() + super.getNumberB(); 9 } 10 } 11 12 /** 13 * 减法运算类 14 */ 15 public class OperationSub extends Operation { 16 @Override 17 public double getResult() { 18 return super.getNumberA() - super.getNumberB(); 19 } 20 } 21 22 /** 23 * 乘法运算类 24 */ 25 public class OperationMul extends Operation { 26 @Override 27 public double getResult() { 28 return super.getNumberA() * super.getNumberB(); 29 } 30 } 31 32 /** 33 * 除法运算类 34 */ 35 public class OperationDiv extends Operation { 36 @Override 37 public double getResult() { 38 return super.getNumberA() / super.getNumberB(); 39 } 40 }
运算静态工厂类
1 /** 2 * 运算静态工厂类 3 */ 4 public class OperationFactory { 5 /** 6 * 创建运算工厂 7 * @param operate 操作符(+ - * /) 8 * @return 匹配运算类 9 */ 10 public static Operation createOperation(String operate) 11 { 12 Operation operation=null; 13 switch (operate) 14 { 15 case "+": 16 operation=new OperationAdd();//创建加法运算类 17 break; 18 case "-": 19 operation=new OperationSub();//创建减法运算类 20 break; 21 case "*": 22 operation=new OperationMul();//创建乘法运算类 23 break; 24 case "/": 25 operation=new OperationDiv();//创建除法运算类 26 break; 27 } 28 return operation; 29 } 30 }
调用端
/** * 大话设计模式(java版) * 第一章:简单工厂模式(静态工厂模式) */ public class StaticFactoryMethodDemo { public static void main(String[] args) { Operation operation = null; double result = 0; operation = OperationFactory.createOperation("+"); operation.setNumberA(1.1); operation.setNumberB(2.2); result = operation.getResult(); System.out.println("加法结果".concat(String.valueOf(result))); operation = OperationFactory.createOperation("-"); operation.setNumberA(3.3); operation.setNumberB(2.2); result = operation.getResult(); System.out.println("减法结果".concat(String.valueOf(result))); operation = OperationFactory.createOperation("*"); operation.setNumberA(3.3); operation.setNumberB(2.2); result = operation.getResult(); System.out.println("乘法结果".concat(String.valueOf(result))); operation = OperationFactory.createOperation("/"); operation.setNumberA(3.3); operation.setNumberB(2.2); result = operation.getResult(); System.out.println("除法结果".concat(String.valueOf(result))); } }
结果
加法结果3.3000000000000003 减法结果1.0999999999999996 乘法结果7.26 除法结果1.4999999999999998