01_SimpleFactoryPattern 计算器实例

大话设计模式书中摘录语句:
喝酒唱歌,人生真爽。
活字印刷
1,要改,只需更改要改之字,此为 可维护
2,这些字并非用完这次就无用,完全可以再后来的印刷中重复使用,此乃 可复用
3,若要加字,只需另刻字加入即可,这是 可扩展
4,字的排列其实可能是横排或竖排,只需将活字移动就可满足排列需求,这是 灵活性好

按照里面的第一章,写了个java版的计算器实例,以便自己更深入理解。

Operation抽象类
package cn;

import java.math.BigDecimal;

public abstract class Operation {
	
	protected BigDecimal dcm;
	protected BigDecimal dcm1;
	protected BigDecimal dcm2;
	
	/**
	 * @param num1
	 * @param num2
	 */
	public Operation(String num1,String num2){
		
		dcm1=new BigDecimal(num1.toCharArray());
		dcm2=new BigDecimal(num2.toCharArray());
	}
	
	public abstract String getResult();
}


修改后的OperationFactory,OperationAdd等不能使用单例。
package cn;

import cn.beans.OperationAdd;
import cn.beans.OperationDiv;
import cn.beans.OperationMul;
import cn.beans.OperationSub;

public class OperationFactory {

	//这样就不会出现双重检查单例的问题了。
	private static OperationFactory instance=new OperationFactory();
	private OperationAdd operationAdd;
	private OperationSub operationSub;
	private OperationMul operatoinMul;
	private OperationDiv operationDiv;
	
	public static OperationFactory getInstance() {
		return instance;
	}
	
	public OperationAdd OperationAdd(String num1,String num2){
		operationAdd= new OperationAdd(num1,num2);
		return operationAdd;
	}
	
	public OperationSub OperationSub(String num1,String num2){
		operationSub= new OperationSub(num1,num2);
		return operationSub;
	}
	
	public OperationMul OperationMul(String num1,String num2){
		operatoinMul= new OperationMul(num1,num2);
		return operatoinMul;
	}
	
	public OperationDiv OperationDiv(String num1,String num2){
		operationDiv= new OperationDiv(num1,num2);
		return operationDiv;
	}
}


加法业务Bean类OperationAdd
package cn.beans;

import cn.Operation;

public class OperationAdd extends Operation {

	public OperationAdd(String num1, String num2) {
		super(num1,num2);		
	}

	@Override
	public String getResult() {		
		dcm = dcm1.add(dcm2);
		return dcm.toString();
	}
}


减法业务Bean类OperationSub
package cn.beans;

import cn.Operation;

public class OperationSub extends Operation{

	public OperationSub(String num1, String num2) {
		super(num1, num2);
	}

	@Override
	public String getResult() {
		dcm = dcm1.subtract(dcm2);
		return dcm.toString();
	}

}


乘法业务Bean类OperationMul
package cn.beans;

import java.math.BigDecimal;

import cn.Operation;

public class OperationMul extends Operation {

	public OperationMul(String num1, String num2) {
		super(num1, num2);
	}

	@Override
	public String getResult() {
		//setScale(保留小数位数,精确方式);
		dcm=dcm1.multiply(dcm2).setScale(3, BigDecimal.ROUND_HALF_UP);
		return dcm.toString();
	}

}


除法业务Bean类OperationDiv
package cn.beans;

import java.math.BigDecimal;

import cn.Operation;

public class OperationDiv extends Operation {

	public OperationDiv(String num1, String num2) {
		super(num1, num2);
	}

	@Override
	public String getResult() {
		if (dcm2.toString().equals("0"))
			return "除数不能为零";
		//商=被除数.divide(除数,保留小数位数,精确方法)   
		dcm=dcm1.divide(dcm2,2,BigDecimal.ROUND_HALF_UP);
		return dcm.toString();
	}

}


还有最后一个JUnit的测试类
package junit.test;

import org.junit.Test;

import cn.Operation;
import cn.OperationFactory;
import cn.beans.OperationDiv;
import cn.beans.OperationMul;
import cn.beans.OperationSub;


public class Test_Calculator {


	@Test public void test(){
		/**
		 * 加减乘除
		 * 测试类
		 */
		String str=null;
		String num1="12.34";
		String num2="43.21";
		//
		Operation operationAdd = OperationFactory.getInstance().OperationAdd(num1, num2);
		str = operationAdd.getResult();
		System.err.println("加法结果1:"+str);
		num1="56.78";
		num2="43.21";
		Operation operationAdd2 = OperationFactory.getInstance().OperationAdd(num1, num2);
		str = operationAdd2.getResult();
		System.err.println("加法结果2:"+str);
		//
		Operation operationSub = OperationFactory.getInstance().OperationSub(num1, num2);
		str = operationSub.getResult();
		System.err.println("减法结果:"+str);
		//
		Operation operationMul = OperationFactory.getInstance().OperationMul(num1, num2);
		str = operationMul.getResult();
		System.err.println("乘法结果:"+str);
		//
		num1="78.90";
		num2="90.12";
		Operation operationMul2 = OperationFactory.getInstance().OperationMul(num1, num2);
		str = operationMul2.getResult();
		System.err.println("乘法结果2:"+str);
		//
		Operation operationDiv = OperationFactory.getInstance().OperationDiv(num1, num2);
		str = operationDiv.getResult();
		System.err.println("除法结果:"+str);
		//
		System.err.println("End.");
		
	}
}


你可能感兴趣的:(java,设计模式,bean,JUnit,UP)