使用策略模式减少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);
    }

 

转载于:https://www.cnblogs.com/SimonHu1993/p/11208814.html

你可能感兴趣的:(使用策略模式减少if else)