大话设计模式笔记 简单工厂模式
书上原版的是C#代码
我比较熟悉java,写的是java代码。
package sampleFactory; public class Operation { protected double _numberA = 0; protected double _numberB = 0; protected double result = 0; public double GetResult() { return result; } }
package sampleFactory; public class OperationAdd extends Operation { @Override public double GetResult() { result=_numberA+_numberB; return result; } }
package sampleFactory; public class OperationFactory { public static Operation createOperate(String operate) { Operation operation=null; switch (operate) { case "+": operation=new OperationAdd(); break; } return operation; } }
package sampleFactory; public class Test { public static void main(String[] args) { Operation oper; oper=OperationFactory.createOperate("+"); oper._numberA=1; oper._numberB=2; double result=oper.GetResult(); System.out.println(result); } }
uml图