大话设计模式学习笔记 -- 策略模式

目录

现金收费抽象类

正常收费子类

打折收费子类

返利收费子类

上下文 策略模式

上下文 策略+简单工厂

客户端代码


现金收费抽象类

/**
 * 现金收费抽象类
 */
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(String moneyRebate){
        this.moneyRebate = Double.parseDouble(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(String moneyCondition,String moneyReturn){
        this.moneyCondition = Double.parseDouble(moneyCondition);
        this.moneyReturn = Double.parseDouble(moneyReturn);
    }

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

上下文 策略模式

/**
 * 策略模式 上下文
 */
public class CashContext {
    private CashSuper cs;
    public CashContext(CashSuper cs){
        this.cs = cs;
    }

    public double getResult(double money){
        return cs.acceptCash(money);
    }
}

上下文 策略+简单工厂

/**
 * 策略与简单工厂结合 上下文
 */
public class CashContext2 {

    CashSuper cs = null;

    public CashContext2(String type) {
        switch (type) {
            case "正常收费":
                cs = new CashNormal();
                break;
            case "满300减100":
                cs = new CashReturn("300", "100");
                break;
            case "打8折":
                cs = new CashRebate("0.8");
                break;
        }
    }

    public double getResult(double money){
        return cs.acceptCash(money);
    }
}

客户端代码

/**
 * 客户端代码
 */
public class TestMain {
    //用于总计
    static double total = 0.0d;
    //单独使用策略模式
    public static void client1(String... args) {
        String selectedCash = args[0];//收费模式
        String price = args[1];//单价
        String num = args[2];//数量

        CashContext cc = null;
        switch (selectedCash){
            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(Double.parseDouble(price)*Double.parseDouble(num));
        total = total + totalPrices;
    }

    static double total2 = 0.0d;
    //策略结合简单工厂
    public static void client2(String... args){
        String selectedCash = args[0];//收费模式
        String price = args[1];//单价
        String num = args[2];//数量

        CashContext2 cc = new CashContext2(selectedCash);
        double totalPrices = 0d;
        totalPrices = cc.getResult(Double.parseDouble(price)*Double.parseDouble(num));
        total2 = total2 + totalPrices;
    }

    public static void main(String[] args) {
        //策略模式
        client1("正常收费","100","1");
        client1("满300减100","80","6");
        client1("打8折","50","10");
        System.out.println(total);

        //策略+简单工厂
        client2("正常收费","100","1");
        client2("满300减100","80","6");
        client2("打8折","50","10");
        System.out.println(total2);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37240709/article/details/81225155
今日推荐