`
xangqun
  • 浏览: 79645 次
  • 性别: Icon_minigender_1
  • 来自: 江西
社区版块
存档分类
最新评论

策略模式java源码实现(大话设计模式学习备忘录)

阅读更多
/**
 * <h1>策略模式</h1>
 * QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
 * @author xangqun
 * 
 */
public class Program {

	/**
	 *PS:
	 * <pre>如果商场搞活动,商品打8折,打5折怎么办??</pre>
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("单价:");
		String strA = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		System.out.println("数量:");
		String strB = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		double numberd = Double.valueOf(strA) + Double.valueOf(strB);
		System.out.println("价格为:" + numberd);

	}

}

/**
 * <h1>策略模式</h1>
 * QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
 * @author xangqun
 * 改进的程序1
 */
public class ProgramTwo {

	/**
	 * PS:3个分支执行的语句除了打折不一样外几乎没什么不同应该考虑重构下
	 * @param args
	 * @throws IOException 
	 * 
	 */
	public static void main(String[] args) throws IOException {
     
		System.out.println("单价:");
		String strA = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		System.out.println("数量:");
		String strB = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
        System.out.println("折扣:");
        String strC = new BufferedReader(new InputStreamReader(System.in))
		.readLine();
        int numc= Integer.valueOf(strC);
        double result = 0;
        switch(numc){
        case 1:result=Double.valueOf(strA) + Double.valueOf(strB);break;
        case 2:result=(Double.valueOf(strA) + Double.valueOf(strB))*0.8;break;
        case 3:result=(Double.valueOf(strA) + Double.valueOf(strB))*0.5;break;
        }
        System.out.println(result);
	}

public abstract class CashSuper {

	public abstract double acceptCash(double money);
}

public class CashNormal extends CashSuper {

	@Override
	public double acceptCash(double money) {

		return money;
	}

}

public class CashRebate extends CashSuper {

	private double moneyRebate=1d;
	public CashRebate(){}
	public CashRebate(String moneyRebate){
		this.moneyRebate=Double.valueOf(moneyRebate);
	}
	@Override
	public double acceptCash(double money) {
		return money*moneyRebate;
	}

}

public class CashReturn extends CashSuper {

	private double moneyCondition=0.0d;
	private double moneyReturn=0.0d;
	public CashReturn(){}
	public CashReturn(double moneyCondition,double moneyReturn){
		this.moneyCondition=moneyCondition;
		this.moneyReturn=moneyReturn;
	}
	@Override
	public double acceptCash(double money) {
		double result=money;
		if(money>moneyCondition){
			result=money-Math.floor(money/moneyCondition)*moneyReturn;
		}
		return result;
	}

}

public class CashFactory {

	public static CashSuper createCashAccept(int type){
		CashSuper cs=null;
		switch(type){
		case 1:cs=new CashNormal();break;
		case 2: cs=new CashReturn(300,100);break;
		case 3:cs=new CashRebate("0.8");break;
		}
		return cs;
	}
}

/**
 * <h1>策略模式</h1>
 * QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
 * @author xangqun
 * 改进的程序2(采用简单工厂模式)
 */
public class ProgramThree {

	/**PS:
	 * 简单工厂模式只是解决对象的创建问题,面对算法时常变动应该考虑用其他方法
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("单价:");
		String strA = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		System.out.println("数量:");
		String strB = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		
		CashSuper cs=CashFactory.createCashAccept(1);
		double totalprices=0d;
		totalprices=cs.acceptCash(Double.valueOf(strA)*Double.valueOf(strB));
		System.out.println(totalprices);
	}

}

public class CashContext {

	private CashSuper cs;
	public CashContext(CashSuper csuper){
		this.cs=csuper;
	}
	public double getResult(double money){
		return cs.acceptCash(money);
	}
}

/**
 * <h1>策略模式</h1>
 * QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
 * @author xangqun
 * 改进的程序3(采用策略模式)
 */
public class ProgramFour {

	/**<b>策略模式(strategy)</b>
	 *<br> 它定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化不会影响到使用算法的客户<br>
	 *策略模式是一种定义一系列算法的方法,从概念上来看,所以这些算法完成的都是相同的工作,只是实现不同,他可以
	 *以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。
	 *优点:1.context定义了一系列的可供重用的算法或行为,继承有助于析取出这些算法中公共功能(这里是getresult)
	 *      2.简化了单元测试因为每个算法都有自己的类,可以通过自己的接口单独测试
	 * 策略模式封装了变化
	 * 应用:策略模式就是用来封装算法的,但实践中,我们发现可以用他来封装几乎任何类型的规则,只要在分析过程中
	 *      听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性     
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("单价:");
		String strA = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		System.out.println("数量:");
		String strB = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		System.out.println("类型:");
		String strC = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		CashContext cc=null;
		switch(Integer.valueOf(strC)){
		case 1:cc=new CashContext(new CashNormal());break;
		case 2: cc=new CashContext(new CashReturn(300,100));break;
		case 3:cc=new CashContext(new CashRebate("0.8"));break;
		}
		double totalprices=0d;
		totalprices=cc.getResult(Double.valueOf(strA)*Double.valueOf(strB));
		System.out.println(totalprices);

	}

public class CashContextFactory {

	private CashSuper cs;
	public CashContextFactory(int type){
		switch(type){
		case 1:this.cs=new CashNormal();break;
		case 2:this.cs=new CashReturn(300,100);break;
		case 3:this.cs=new CashRebate("0.8");break;
		}
	}
	public double getResult(double money){
		return cs.acceptCash(money);
	}
}

/**
 * <h1>策略模式</h1>
 * QS:做个商场收银软件,营销员根据客户所购买的商品和数量向客户收费 第一次程序
 * @author xangqun
 * 改进的程序3(采用策略模式+简单工厂模式)
 */
public class ProgramFive {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("单价:");
		String strA = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		System.out.println("数量:");
		String strB = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		System.out.println("类型:");
		String strC = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		CashContextFactory cc=new CashContextFactory(Integer.valueOf(strC));
		double totalprices=cc.getResult(Double.valueOf(strA)*Double.valueOf(strB));
		System.out.println(totalprices);
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics