用工厂方法模式实现四则计算器
-
结构类图
实例实现代码
步骤1: 创建抽象工厂类 zc32OperationFactory,定义具体工厂的公共接口
interface OperationFactory { // 工厂接口
public static Operation getResult(String operator) {
Operation oper=null;
switch(operator) {
case"+":
oper=new AddOperation();
break;
case"-":
oper=new SubOperation();
break;
case"*":
oper=new ChengOperation();
break;
case"/":
oper=new ChuOperation();
break;
}
return oper;
}
}
步骤2: 创