(十五)策略模式

水果要搞優(yōu)惠活動(dòng),但是優(yōu)惠活動(dòng)有很多,同樣的不影響主流程的情況下,可以選擇不同的優(yōu)惠策略來計(jì)算最終的消費(fèi)金額


優(yōu)惠策略

策略模式

需要定義好統(tǒng)一的功能入口,注入不同的實(shí)現(xiàn)類來實(shí)現(xiàn)不同的優(yōu)惠功能


策略模式
public abstract class ShoppingCart {
    private Discount discount;
    private List<Fruit> products = new ArrayList<>();

    public ShoppingCart(List<Fruit> products){
        this.products = products;
    }

    public void setDiscount(Discount discount) {
        this.discount = discount;
    }

    //提交訂單主流程
    public void submitOrder(){
        //計(jì)算商品金額
        int money = balance();
        System.out.println("商品總金額為:"+money+"元");

        //優(yōu)惠減免
        money = discount.calculate(money);
        System.out.println("優(yōu)惠減免后:"+ money+"元,");

        //保存訂單
        pay(money);

        //送貨上門
        sendHome();

    }

    //計(jì)算金額
    private int balance(){
        int money = 0;
        System.out.print("商品清單:");
        for (Fruit fruit : products){
            fruit.draw();
            System.out.print(",");
            money += fruit.price();
        }
        return money;
    }

    private void sendHome(){
        System.out.println("三公里以內(nèi),免費(fèi)送貨上門");
    }

    //提交保存
    protected abstract void pay(int money);

}

public interface Discount {
    public int calculate(int money);
}

定義Discount 接口,實(shí)現(xiàn)其計(jì)算金額的方法

public class FullDiscount implements Discount {
    @Override
    public int calculate(int money) {
        if (money > 200){
            System.out.println("優(yōu)惠減免20元");
            return money - 20;
        }
        return money;
    }
}
/**
 * 第二單9折優(yōu)惠
 */
public class SecondDiscount implements Discount {
    @Override
    public int calculate(int money) {
        Double balance =  money * 0.9;
        return balance.intValue();
    }
}

注入具體的實(shí)現(xiàn)類,最后調(diào)用接口方法即可

/**
 * 模板方法模式
 * 訂單費(fèi)用結(jié)算過程
 */
public class ShoppingCartClient {

    private static Map<String,Discount> disCounts = new HashMap();
    static {
        disCounts.put("full",new FullDiscount());
        disCounts.put("newer",new NewerDiscount());
        disCounts.put("second",new SecondDiscount());
    }

    public static void main(String[] args) {
        List<Fruit> products = new ArrayList();

        products.add(StaticFactory.getFruitApple());
        products.add(StaticFactory.getFruitBanana());
        products.add(StaticFactory.getFruitOrange());

        ShoppingCart cart = new OtherPayShopping(products);

        //注入優(yōu)惠方案
        String discount = "second";
        cart.setDiscount(disCounts.get(discount));
        cart.submitOrder();
    }
}

其核心思想就是注入不同的實(shí)現(xiàn)類,來實(shí)現(xiàn)不同功能,既不需要修改主流程,也可以隨意擴(kuò)展(開閉原則),這其實(shí)就是spring的IOC思想

總結(jié)

  • 定一個(gè)功能接口,通過注入的方式,來實(shí)現(xiàn)不同的功能
  • 適用于需要使用不同的策略來實(shí)現(xiàn)功能的場(chǎng)景
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容