简介
用一个单独的类来做创造实例的过程
简单运算工厂类
示例是一个计算器,我省略了各个计算类的细节。
package 简单工厂;
public class OperationFactory {
public static Operation createOperate(String operate){
Operation oper=null;
switch(operate){
case"+":
oper=new OperationAdd();
break;
case"-":
oper=new OperationSub();
break;
case"*":
oper=new OperationMul();
break;
case"/":
oper=new OperationDiv();
break;
}
}
}
class Operation{}
class OperationAdd extends Operation{}
class OperationSub extends Operation{}
class OperationMul extends Operation{}
class OperationDiv extends Operation{}