[大话设计模式]学习笔记——策略模式

需求:

简单工厂模式实现:

1.收费方式抽象类
 
public abstract class CashSuper {

    /**
     * 现金收取方式的抽象方法
     * @param money 原价
     * @return 当前价
     */
    protected abstract double acceptCash(double money);
}

 

2.普通收费模式
 
public class CashNormal extends CashSuper {
    @Override
    protected double acceptCash(double money) {
        return money;
    }
}

3.打折收费子类
public class CashRebate extends CashSuper {

    private final double moneyRebate;

    public CashRebate(String moneyRebate){
        this.moneyRebate = Double.parseDouble(moneyRebate);

    }

    @Override
    protected double acceptCash(double money) {
        return money*moneyRebate;
    }
}
 
4.返利收费子类
public class CashReturn extends CashSuper {

    private final double moneyReturn;
    private final double moneyCondition;

    public CashReturn(String moneyCondition, String moneyReturn){

        this.moneyCondition = Double.parseDouble(moneyCondition);
        this.moneyReturn = Double.parseDouble(moneyReturn);
    }

    @Override
    protected double acceptCash(double money) {
        double result = money;
        if(money>=moneyCondition){
            result = money-(money/moneyCondition)*moneyReturn;
        }
        return result;
    }
}
 
5.工厂类
public class CashFactory {
    public static CashSuper createCashAccept(String type){
        CashSuper cashSuper = null;
        switch (type) {
            case "正常收费":
                CashNormal cashNormal = new CashNormal();
                cashSuper = cashNormal;
                break;
            case "满300减100":
                CashReturn cashReturn = new CashReturn("300", "100");
                cashSuper = cashReturn;
                break;
            case "打7折":
                CashRebate cashRebate = new CashRebate("0.7");
                cashSuper = cashRebate;
                break;
        }
        return cashSuper;
    }
}

 

6.客户端代码
    public void btnOk_Click(View view){
        CashSuper cashNomal = CashFactory.createCashAccept("正常收费");
        double result1 = cashNomal.acceptCash(12.5 * 5);
    }

 

代码结构图:

策略模式:

让算法的变化,不会影响到使用算法的变化
 
客户端代码
 

策略模式和简单工厂模式结合实现

CashSuper及其3个子类都是不用变
1.cashContext类  策略模式和简单工厂模式的结合
public class CashContext {

    CashSuper cashSuper = null;

    public CashContext(String type){
        cashSuper = null;
        switch (type) {
            case "正常收费":
                CashNormal cashNormal = new CashNormal();
                cashSuper = cashNormal;
                break;
            case "满300减100":
                CashReturn cashReturn = new CashReturn("300", "100");
                cashSuper = cashReturn;
                break;
            case "打7折":
                CashRebate cashRebate = new CashRebate("0.7");
                cashSuper = cashRebate;
                break;
        }

    }
    public double getResult(double money) {
        double cashResult = cashSuper.acceptCash(money);
        return cashResult;
    }
}

 

2.客户端代码变为
    public void btnOk_Click(View view){
        CashContext cashContext = new CashContext("正常收费");
        double result = cashContext.getResult(12.5 * 5);
    }

 

分析:
对比简单工厂设计模式

策略模式解析:

 



posted @ 2016-04-07 13:48  吹泡泡`OOO  阅读(234)  评论(0编辑  收藏  举报