简单工厂实现-大话设计模式

类结构图:简单工厂实现-大话设计模式_第1张图片

简单工厂实现-大话设计模式_第2张图片

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;
	}
}



你可能感兴趣的:(设计模式,String,Class,interface)