• 使用策略模式减少if else


    首先要明确的说出策略模式会不可避免导致你的代码类变得很多,如果对应方法逻辑很复杂时可采用,如果逻辑不是很复杂就有点大材小用了。

    package com.zihexin.application.strategy;
    
    /**
     * @Author: SimonHu
     * @Date: 2019/7/18 15:38
     * @Description:定义策略模式
     */
    public interface Strategy {
        void consumeTask();
        
        void refundTask();
        
        void sqtConsumeTask();
        
        void sqtRefundTask();
    }
    package com.zihexin.application.strategy.strategyImpl;
    
    import com.zihexin.application.strategy.SpringUtils;
    import com.zihexin.application.strategy.Strategy;
    import com.zihexin.application.task.BaiDuWaiMaiTask;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author: SimonHu
     * @Date: 2019/7/18 15:40
     * @Description:
     */
    @Component
    public class BaiDuWaiMaiStrategy implements Strategy {
        @Autowired
        private BaiDuWaiMaiTask baiDuWaiMaiTask;
        
        public BaiDuWaiMaiStrategy() {
            baiDuWaiMaiTask = SpringUtils.getBean("baiDuWaiMaiTask");
        }
        
        @Override
        public void consumeTask() {
            baiDuWaiMaiTask.consumeTask();
        }
        
        @Override
        public void refundTask() {
            baiDuWaiMaiTask.refundTask();
        }
        
        @Override
        public void sqtConsumeTask() {
        }
        
        @Override
        public void sqtRefundTask() {
        }
    }
    package com.zihexin.application.strategy;
    
    import com.zihexin.application.strategy.strategyImpl.*;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @Author: SimonHu
     * @Date: 2019/7/18 15:42
     * @Description:策略工厂,负责Strategy实例的创建,避免if else判断
     */
    public class StrategyFactory {
        private static StrategyFactory factory = new StrategyFactory();
        
        private StrategyFactory() {
        }
        
        private static Map strategyMap = new HashMap<>();
        
        static {
            strategyMap.put("daMaiTask", new DmStrategy());
            strategyMap.put("baiDuWaiMaiTask", new BaiDuWaiMaiStrategy());
            strategyMap.put("elmTask", new ElmStrategy());
            strategyMap.put("maoYanTask", new MaoYanStrategy());
            strategyMap.put("meidianTask", new MeidianStrategy());
            strategyMap.put("tppTask", new TPPStrategy());
            strategyMap.put("wuBaTask", new WuBaStrategy());
        }
        
        public Strategy creator(String type) {
            return (Strategy) strategyMap.get(type);
        }
        
        public static StrategyFactory getInstance() {
            return factory;
        }
    }
    package com.zihexin.application.strategy;
    
    /**
     * @Author: SimonHu
     * @Date: 2019/7/18 15:42
     * @Description:创建环境角色Context:
     */
    public class Context {
        private Strategy strategy;
        
        public void consumeTask(String type) {
            strategy = StrategyFactory.getInstance().creator(type);
            strategy.consumeTask();
        }
        
        public void refundTask(String type) {
            strategy = StrategyFactory.getInstance().creator(type);
            strategy.refundTask();
        }
        
        public void sqtConsumeTask(String type) {
            strategy = StrategyFactory.getInstance().creator(type);
            strategy.sqtConsumeTask();
        }
        
        public void sqtRefundTask(String type) {
            strategy = StrategyFactory.getInstance().creator(type);
            strategy.sqtRefundTask();
        }
        
        public Strategy getStrategy() {
            return strategy;
        }
        
        public void setStrategy(Strategy strategy) {
            this.strategy = strategy;
        }
    }
    //调用
    public void testst(String type) {
            Context context = new Context();
            context.consumeTask(type);
            context.refundTask(type);
            context.sqtConsumeTask(type);
            context.sqtRefundTask(type);
        }
  • 相关阅读:
    Codevs_1403_新三国争霸_(Kruskal+动态规划)
    BZOJ_1084_[SCOI2005]_最大子矩阵_(动态规划)
    BZOJ_1180_[CROATIAN2009]_OTOCI_(LCT)
    BZOJ_1611_[Usaco2008_Feb]_Meteor_Shower流星雨_(bfs)
    hdu5338 (二进制,双指针)
    并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)
    生成树专题
    生成树相关问题
    uvaLive5713 次小生成树
    如何将无向图变为点/边双连通,如何将有向图变为强连通图
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/11208814.html
Copyright © 2020-2023  润新知