zoukankan      html  css  js  c++  java
  • SpringBoot结合策略模式

    结合SpringBoot的策略模式套路,代码少些if-else

    1、编写接口

    /**
     * 计算策略接口
     * @author xuchen
     * @Date: 2021/3/8 19:07
     */
    public interface CalculateStrategy {
        int doOperation(int num1,int num2);
    }

     2、实现类

    /**
     * @author xuchen
     * @Date: 2021/3/8 19:08
     */
    @Component
    public class AddOperation implements CalculateStrategy {
        @Override
        public int doOperation(int num1, int num2) {
            return num1 + num2;
        }
    }
    
    /**
     * @author xuchen
     * @Date: 2021/3/8 19:09
     */
    @Component
    public class MultiplyOperation implements CalculateStrategy {
        @Override
        public int doOperation(int num1, int num2) {
            return num1 * num2;
        }
    
    }
    
    /**
     * @author xuchen
     * @Date: 2021/3/8 19:09
     */
    @Component
    public class SubstractOperation implements CalculateStrategy {
        @Override
        public int doOperation(int num1, int num2) {
            return num1 - num2;
        }
    
    }

     3、上下文编写

    创建上下文管理,用于提取策略

    import com.google.common.base.Preconditions;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    /**
     * 创建上下文管理,用于提取策略。
     * @autowired注解放的位置和对应的参数列表
     * 实际上它还可以注入到Map和List,Map的key就是它注入时的名,List则是存放全部实例对象
     * @author xuchen
     * @Date: 2021/3/8 19:10
     */
    @Component
    public class CalculatelOperationContext {
        private final Map<String, CalculateStrategy> strategyMap = new ConcurrentHashMap<>();
    
        @Autowired
        public void stragegyInteface(Map<String, CalculateStrategy> strategyMap) {
            this.strategyMap.clear();
            strategyMap.forEach(this.strategyMap::put);
            System.out.println(this.strategyMap);
        }
    
    
        @Autowired
        public void stragegyInteface2(List<CalculateStrategy> strategyMap) {
            strategyMap.forEach(System.out::println);
        }
    
        public CalculateStrategy strategySelect(String mode) {
            Preconditions.checkArgument(!StringUtils.isEmpty(mode), "不允许输入空字符串");
            return this.strategyMap.get(mode);
        }
    }

    4、测试controller (此处使用了swagger,可以去除此块)

    import com.xc.luckysheet.utils.strategy.CalculatelOperationContext;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author xuchen
     * @Date: 2021/3/8 20:01
     */
    @Slf4j
    @RestController
    @Api(description = "测试接口")
    @RequestMapping("test/Strategy")
    public class TestStrategyController {
    
        @Autowired
        private CalculatelOperationContext calculatelOperationContext;
    
        @ApiOperation(value = "Strategy查询",notes = "Strategy查询")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "mode", value = "multiplyOperation", paramType = "query", required = true, dataType = "String")
        })
        @GetMapping(value = "/operation")
        public String strategySelect(@RequestParam("mode") String mode) {
            return String.valueOf(calculatelOperationContext.strategySelect(mode).doOperation(20, 5));
        }
    }

    5、测试结果

     

  • 相关阅读:
    Java实现第九届蓝桥杯付账问题
    Java实现第九届蓝桥杯付账问题
    Java实现第九届蓝桥杯星期一
    Java实现第九届蓝桥杯星期一
    Java实现第九届蓝桥杯倍数问题
    Java实现第九届蓝桥杯倍数问题
    Java实现第九届蓝桥杯倍数问题
    Java实现第九届蓝桥杯倍数问题
    Java实现第九届蓝桥杯倍数问题
    Java实现第九届蓝桥杯三体攻击
  • 原文地址:https://www.cnblogs.com/xuchen0117/p/14503658.html
Copyright © 2011-2022 走看看