首页 > 代码库 > 学习大话设计模式01_简单工厂模式(简易计算器)
学习大话设计模式01_简单工厂模式(简易计算器)
1 /** 2 * 面向对象(运算类) 3 * 简易计算器 4 * @author Monica 5 * 6 */ 7 public class Operation { 8 private double numberA = 0; 9 private double numberB = 0;10 11 //虚函数12 public double GetResult() {13 double result = 0;14 return result;15 }16 17 public double getNumberA() {18 return numberA;19 }20 21 public void setNumberA(double numberA) {22 this.numberA = numberA;23 }24 25 public double getNumberB() {26 return numberB;27 }28 29 public void setNumberB(double numberB) {30 this.numberB = numberB;31 }32 }
1 /** 2 * 加法 3 * @author Monica 4 * 5 */ 6 public class OperationAdd extends Operation { 7 8 /** 9 * 继承运算类10 */11 @Override12 public double GetResult() {13 double result = 0;14 result = getNumberA() + getNumberB();15 return super.GetResult();16 }17 }
1 /** 2 * 简单工厂模式 3 * @author Administrator 4 * 5 */ 6 public class OperationFactory { 7 8 public static Operation createOperate(char operate) { 9 Operation oper = null;10 switch (operate) {11 case ‘+‘:12 oper = new OperationAdd();13 break;14 }15 return oper;16 }17 }
1 public class Main { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 Operation oper; 8 oper = OperationFactory.createOperate(‘+‘); 9 oper.setNumberA(1);10 oper.setNumberB(2);11 double result = oper.GetResult();12 }13 14 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。