美文网首页
设计模式---策略模式

设计模式---策略模式

作者: _非_阳_ | 来源:发表于2019-03-06 17:41 被阅读0次
    image.png

    只是自己日常做一些笔记

    采用策略模式模拟一次专卖店的计价操作

    Strategy类
    package com.mjt.learning.pattern.strategy;
    
    /**
     * Copyright:Learning
     * Author: liyang <br>
     * Date:2019/3/6 5:17 PM<br>
     * Desc:定义计算价格的接口 <br>
     */
    public interface Strategy {
        /**
         * @param goodsPrice 原价
         * @return 实际价格
         */
        double calcPrice(double goodsPrice);
    }
    
    

    具体的计算策略
    普通用户的购物计价规则

    package com.mjt.learning.pattern.strategy;
    
    /**
     * Copyright:Learning
     * Author: liyang <br>
     * Date:2019/3/6 5:19 PM<br>
     * Desc: 普通用户的价格计算策略<br>
     */
    public class NormalCustomerStrategy implements Strategy {
        @Override
        public double calcPrice(double goodsPrice) {
            System.out.println("普通用户没有折扣");
            return goodsPrice;
        }
    }
    

    VIP用户

    package com.mjt.learning.pattern.strategy;
    
    /**
     * Copyright:Learning
     * Author: liyang <br>
     * Date:2019/3/6 5:20 PM<br>
     * Desc:VIP的价格计算策略 <br>
     */
    public class VipStrategy implements Strategy {
        @Override
        public double calcPrice(double goodsPrice) {
            System.out.println("VIP客户价格打八折");
            return goodsPrice * 0.8;
        }
    }
    

    超级VIP

    package com.mjt.learning.pattern.strategy;
    
    /**
     * Copyright:Learning
     * Author: liyang <br>
     * Date:2019/3/6 5:21 PM<br>
     * Desc: 超级VIP计算策略<br>
     */
    public class SVipStrategy implements Strategy {
        @Override
        public double calcPrice(double goodsPrice) {
            System.out.println("超级VIP用户7折!");
            return goodsPrice * 0.7;
        }
    }
    

    Calculator计算器

    package com.mjt.learning.pattern.strategy;
    
    /**
     * Copyright:Learning
     * Author: liyang <br>
     * Date:2019/3/6 5:25 PM<br>
     * Desc: 相当于Context<br>
     */
    public class Calculator {
        /**
         * Context 持有一个策略对象
         */
        private Strategy strategy = null;
    
        public Calculator(Strategy strategy) {
            this.strategy = strategy;
        }
    
        /**
         * 原价
         *
         * @param originPrice 商品原价
         * @return 不同用户的对应的价格
         */
        public double calcPrice(double originPrice) {
            return strategy.calcPrice(originPrice);
        }
    
        public void changeStrategy(Strategy strategy) {
            this.strategy = strategy;
        }
    }
    

    Client

    package com.mjt.learning.pattern.strategy;
    
    /**
     * Copyright:Learning
     * Author: liyang <br>
     * Date:2019/3/6 5:28 PM<br>
     * Desc: <br>
     */
    public class Client {
        public static void main(String[] args) {
            double price=100d;
            Strategy strategy=new NormalCustomerStrategy();
            Calculator calculator=new Calculator(strategy);
            System.out.println(calculator.calcPrice(price));
    
            strategy=new VipStrategy();
            calculator.changeStrategy(strategy);
            System.out.println(calculator.calcPrice(price));
    
            strategy=new SVipStrategy();
            calculator.changeStrategy(strategy);
            System.out.println(calculator.calcPrice(price));
        }
    }
    

    打印结果

    普通用户没有折扣
    100.0
    VIP客户价格打八折
    80.0
    超级VIP用户7折!
    70.0

    相关文章

      网友评论

          本文标题:设计模式---策略模式

          本文链接:https://www.haomeiwen.com/subject/jwyxpqtx.html