策略模式

来源:互联网 发布:高速公路收费计算软件 编辑:程序博客网 时间:2024/06/05 08:53

通过商场促销来学习策略模式。

商场促销的可能方式,打折,满200返50等等。

1、抽象父类CashSuper.java,定义一个抽象方法。

public abstract class CashSuper{/** * @param money商品原价 * @return 商品实际接收的钱 */public abstract double acceptCash(double money);}

2、正常收费的子类CashNormal.java。

public class CashNormal extends CashSuper{    /*     * 正常收费返回原价     */    @Override    public double acceptCash(double money)    {         return money;    }}

3、打折的子类CashRebate.java

public class CashRebate extends CashSuper{    private double moneyRebate=1d;    public CashRebate(String moneyRebate)    {        this.moneyRebate=Double.parseDouble(moneyRebate);    }    @Override    public double acceptCash(double money)    {        // TODO Auto-generated method stub        return moneyRebate*money;    }}

3、返利子类CashReturn。

public class CashReturn extends CashSuper{    private double moneyCondition=0.0d;    private double moneyReturn=0.0d;        public CashReturn(String moneyCondition,String moneyReturn)    {        this.moneyCondition=Double.parseDouble(moneyCondition);        this.moneyReturn=Double.parseDouble(moneyReturn);    }    @Override    public double acceptCash(double money)    {        // TODO Auto-generated method stub        double result=money;        if(money>=moneyCondition)        {            result=money-Math.floor(money/moneyCondition)*moneyReturn;        }        return result;    }}

3、策略模式的实现CashContext.java

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

4、客户端测试test.java

public class test{public static void main(String[] args){CashContext cc=null;String type="正常收费";//type的值在实际中应该从前端获得数据。switch (type){case "正常收费":cc=new CashContext(new CashNormal());break;case "满300返100":cc=new CashContext(new CashReturn("300","100"));break;case "打8折":cc=new CashContext(new CashRebate("0.8"));break;}double totalPrices=0d;totalPrices=cc.GetResult(300);System.out.println(totalPrices);}}

上述代码还存在一点缺陷,就是判断促销方式的过程是在客户端完成的。

策略和工厂模式的结合,可以将这个过程进行转移。

代码修改如下:

5、CahContext.java

public class CashContext{    private CashSuper cs;    /**     * @param csuper     *            通过构造函数传递具体的收费策略     *            工厂模式与策略模式的简单组合     */    public CashContext(String type)    {        switch (type)        {        case "正常收费":            CashNormal cs0 = new CashNormal();            this.cs = cs0;            break;        case "满300返100":            CashReturn cs1 = new CashReturn("300", "100");            this.cs = cs1;            break;        case "打8折":            CashRebate cs2 = new CashRebate("0.8");            this.cs = cs2;            break;        }    }    public double GetResult(double money)    {        return cs.acceptCash(money);    }}

6、程序测试如下,test.java

public class test{public static void main(String[] args){    new Show();    CashContext cc=new CashContext("打8折");    double result=cc.GetResult(1000);    System.out.println("实际返回的值:"+result);}}




0 0
原创粉丝点击