策略模式----Java

策略模式:就是针对一个问题在不同的情形下有不同的解决算法,为了灵活处理,可增减算法,就是用策略模式

感觉和简单工厂模式有些形似。。。。。


例:超市收银问题

针对不同的情况,有不打折的,打折的,还有满300返100的,等等。。。。


1、建个接口,它是各种模式抽象出来的公共方法


/**
 *Jun 28, 2013
 *Copyright(c)JackWang
 *All rights reserve
 *@Author <a href="mailto:wangchengjack@163.com">JackWang</a>
*/
package com.example.strategy;

public interface CashSuper {
	public Double getReuslt(Double money);

}


2、各个算法的实现类


/**
 *Jun 28, 2013
 *Copyright(c)JackWang
 *All rights reserve
 *@Author <a href="mailto:wangchengjack@163.com">JackWang</a>
*/
package com.example.strategy;

public class CashNormal implements CashSuper{

	@Override
	public Double getReuslt(Double money) {
		return money;
	}
	

}


/**
 *Jun 28, 2013
 *Copyright(c)JackWang
 *All rights reserve
 *@Author <a href="mailto:wangchengjack@163.com">JackWang</a>
*/
package com.example.strategy;

public class CashRebate implements CashSuper{
	private Double rebate = 0.8;

	public Double getRebate() {
		return rebate;
	}


	public void setRebate(Double rebate) {
		this.rebate = rebate;
	}

	/**
	 * @param rebate
	 */
	public CashRebate(Double rebate) {
		super();
		this.rebate = rebate;
	}


	@Override
	public Double getReuslt(Double money) {
		return money*getRebate();
	}
	

}

/**
 *Jun 28, 2013
 *Copyright(c)JackWang
 *All rights reserve
 *@Author <a href="mailto:wangchengjack@163.com">JackWang</a>
*/
package com.example.strategy;

public class CashReturn implements CashSuper{
	
	private Double original;

	private Double rebateMoney;

	public Double getOriginal() {
		return original;
	}

	public void setOriginal(Double original) {
		this.original = original;
	}

	public Double getRebateMoney() {
		return rebateMoney;
	}

	public void setRebateMoney(Double rebateMoney) {
		this.rebateMoney = rebateMoney;
	}

	/**
	 * @param original
	 * @param rebateMoney
	 */
	public CashReturn(Double original, Double rebateMoney) {
		super();
		this.original = original;
		this.rebateMoney = rebateMoney;
	}

	@Override
	public Double getReuslt(Double money) {
		if (money >= getOriginal()) {
			return money - getRebateMoney();
		}
		
		return money;
	}
	

}

3、策略选择


/**
 *Jun 28, 2013
 *Copyright(c)JackWang
 *All rights reserve
 *@Author <a href="mailto:wangchengjack@163.com">JackWang</a>
*/
package com.example.strategy;

public class StrategyContext {
	CashSuper cashSuper = null;

	/**
	 * @param cashSuper
	 */
	public StrategyContext(int i) {
		switch (i) {
		case 1:
			cashSuper = new CashRebate(0.6);
			break;
		case 2:
			cashSuper = new CashReturn(300.0,100.0);
			break;

		default:
			cashSuper = new CashNormal();
			break;
		}
	}
	
	public Double getMoney(Double money){
		return cashSuper.getReuslt(money);
	}
	

}

4、测试

/**
 *Jun 28, 2013
 *Copyright(c)JackWang
 *All rights reserve
 *@Author <a href="mailto:wangchengjack@163.com">JackWang</a>
*/
package com.example.strategy;

public class TestStrategy {
	public static void main(String[] args) {
		StrategyContext sc1 = new StrategyContext(0);
		System.out.println(sc1.getMoney(500.0));
		
		StrategyContext sc2 = new StrategyContext(1);
		System.out.println(sc2.getMoney(600.0));
		
		StrategyContext sc3 = new StrategyContext(2);
		System.out.println(sc3.getMoney(500.0));
		
	}

}

-------------------------------

经过学习改编自《大话设计模式》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值