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

类结构图:

简单工厂实现-大话设计模式_第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;
	}
}



本文转自:http://blog.csdn.net/wxwzy738/article/details/7592392

你可能感兴趣的:(简单工厂实现-大话设计模式)