借助Spring优雅的使用策略模式
优雅的借助Spring使用策略模式
策略模式(Strategy Pattern)
一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。
在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。
example
策略接口
public interface Strategy {
public int doOperation(int num1, int num2);
}
实现策略接口
// +
public class OperationAdd implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}
// -
public class OperationSubstract implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}
// *
public class OperationMultiply implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 * num2;
}
}
创建 Context 类
public class Context {
private Strategy strategy;
public Context(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
public static void main(String[] args) {
Context context = new Context(new OperationAdd());
System.out.println("10+5="+context.executeStrategy(10, 5));
context = new Context(new OperationSubstract());
System.out.println("10-5="+context.executeStrategy(10, 5));
context = new Context(new OperationMultiply());
System.out.println("10*5="+context.executeStrategy(10, 5));
}
}
执行结果
10+5=15
10-5=5
10*5=50
使用场景
我们看下面这个例子
public String event(){
if("level 0"){
return "do this";
}
if("level 1"){
return "do that";
}
if("level 2"){
return "do where";
}
if("level 3"){
return "do what";
}
if("level 4"){
if("allen is handsome"){
return "do you like me?";
}
return "do you know?Where is the hospital?";
}
}
这里只是举个例子, 真实业务场景可能比这个复杂的多, 显然使用策略模式重构这套逻辑更适合程序的维护和开发.
那有些机灵的观客老爷就会问了, 那Spring里面怎么用才会更优雅呢?
Spring 实现策略模式
策略接口
public interface LevelRank {
String doEvent(String face);
}
策略实现
@Component("AnyValue")
@Order(4)
public class Level0 implements LevelRank {
@Override
public String doEvent(String face) {
return "do this";
}
}
@Component
@Order(3)
public class Level1 implements LevelRank {
@Override
public String doEvent(String face) {
return "do that";
}
}
@Component
@Order(2)
public class Level2 implements LevelRank {
@Override
public String doEvent(String face) {
return "do where";
}
}
@Component
@Order(1)
public class Level3 implements LevelRank {
@Override
public String doEvent(String face) {
return "do what";
}
}
@Component
@Order(0)
public class Level4 implements LevelRank {
@Override
public String doEvent(String face) {
if(StringUtils.equals(face, "handsome")){
return "do you like me?";
}
return "do you know?Where is the hospital?";
}
这里注意
- @Component的value 值是随意的, 但要有实际意义
- @Order可以觉得该Bean的加载顺序
Context的实现
@Slf4j
@RestController
public class LevelController {
// 展示一下list的用法
@Resource
private List<LevelRank> list;
@Resource
private Map<String, LevelRank> map;
@GetMapping("level")
public void doEvent(String level, String face){
log.info(String.valueOf(map));
LevelRank levelRank = map.get(level);
log.info(levelRank.doEvent(face));
//// 展示一下list的用法
log.info("===========================");
for (LevelRank rank : list) {
log.info(rank.doEvent(face));
}
}
}
我们看下结果:
ok, 差不多就这些了.欢迎评论,交流点赞