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

设计模式-策略(Strategy)模式

作者: qyfl | 来源:发表于2019-11-12 17:54 被阅读0次

    主要角色

    • 策略
    • 具体策略

    职责

    具体算法解决具体的问题,策略模式可以整体替换算法的实现,使用不同的算法解决同一个问题。

    类图

    类关系示意图

    代码

    public interface PromotionStrategy {
        void doPromotion();
    }
    ---
    public class FanXianPromotionStrategy implements PromotionStrategy {
        @Override
        public void doPromotion() {
            System.out.println("返现促销,返回的金额存放到慕课网用户的余额中");
        }
    }
    ---
    public class EmptyPromotionStrategy implements PromotionStrategy {
        @Override
        public void doPromotion() {
            System.out.println("无促销活动");
        }
    }
    ---
    public class LiJianPromotionStrategy implements PromotionStrategy {
        @Override
        public void doPromotion() {
            System.out.println("立减促销,课程的价格直接减去配置的价格");
        }
    }
    ---
    public class ManJianPromotionStrategy implements PromotionStrategy{
        @Override
        public void doPromotion() {
            System.out.println("满减促销,满200-20元");
        }
    }
    ---
    public class PromotionActivity {
        private PromotionStrategy promotionStrategy;
    
        public PromotionActivity(PromotionStrategy promotionStrategy) {
            this.promotionStrategy = promotionStrategy;
        }
    
        public void executePromotionStrategy(){
            promotionStrategy.doPromotion();
        }
    }
    ---
    public class PromotionStrategyFactory {
        private static Map<String,PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String, PromotionStrategy>();
        static {
            PROMOTION_STRATEGY_MAP.put(PromotionKey.LIJIAN,new LiJianPromotionStrategy());
            PROMOTION_STRATEGY_MAP.put(PromotionKey.FANXIAN,new FanXianPromotionStrategy());
            PROMOTION_STRATEGY_MAP.put(PromotionKey.MANJIAN,new ManJianPromotionStrategy());
        }
    
        private static final PromotionStrategy NON_PROMOTION = new EmptyPromotionStrategy();
    
        private PromotionStrategyFactory(){
    
        }
    
        public static PromotionStrategy getPromotionStrategy(String promotionKey){
            PromotionStrategy promotionStrategy = PROMOTION_STRATEGY_MAP.get(promotionKey);
            return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;
        }
    
        private interface PromotionKey{
            String LIJIAN = "LIJIAN";
            String FANXIAN = "FANXIAN";
            String MANJIAN = "MANJIAN";
        }
    }
    

    使用

    public static void main(String[] args) {
           String promotionKey = "MANJIANxxx";
           PromotionActivity promotionActivity = new PromotionActivity(PromotionStrategyFactory.getPromotionStrategy(promotionKey));
           promotionActivity.executePromotionStrategy();
       }
    

    技巧

    • 策略模式非常适合替代一长串的 if else。
    • 策略模式简单的理解就是,要实现数组排序的功能,具体的实现有很多种,这时候就可以用策略模式。

    相关文章

      网友评论

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

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