类结构图:
public static void main(String[] args) {
// Operation oper=OperationFactory.createOperation("+");
Operation oper=OperationFactory.createOperation("-");
System.out.println(oper.getResult(7, 8));
/**
* -1.0
*/
}
public class OperationFactory {
public static Operation createOperation(String type){
Operation oper=null;
if("+".equals(type)){
oper=new OpeationAdd();
}else if("-".equals(type)){
oper=new OperationSub();
}
return oper;
}
}
//计算接口类
public interface Operation {
double getResult(double i,double j);
}
public class OpeationAdd implements Operation{
public double getResult(double i, double j) {
return i+j;
}
}
public class OperationSub implements Operation{
public double getResult(double i, double j) {
return i-j;
}
}