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、测试结果

 

 

 

 

 

 

 

 

 

posted @ 2021-03-09 09:19  桥头堡洗脚城  阅读(282)  评论(0编辑  收藏  举报