策略模式(如何干掉 if else)

策略模式

1.介绍

策略模式定义了一系列的算法,并将每一个算法封装起来,使每个算法可以相互替代,使算法本身和使用算法的客户端分割开来,相互独立。

策略模式基于的一种开闭原则

开闭原则:  
  对于扩展是开放的(Open for extension)。这意味着模块的行为是可以扩展的。当应用的需求改变时,我们可以对模块进行扩展,使其具有满足那些改变的新行为。也就是说,我们可以改变模块的功能。  
  对于修改是关闭的(Closed for modification)。对模块行为进行扩展时,不必改动模块的源代码或者二进制代码。

2.理论实际

在很多过程中,一个代码块需要使用很多if-else来实现我们的现有逻辑。
如:

public BigDecimal quote(String type){
    if ("第一种实现".equals(type)) {
        return this.firstImpl();
    }else if ("第二种实现".equals(type)) {
        return this.secondmpl();
    }else if("第三种实现".equals(type)){
        return this.thirdImpl();
    }
    return null;
}

虽然这个看上去不是很复杂,用if-else还能结构,但是如果当中的业务逻辑复杂的话那就会非常糟糕;而且如果当中需要添加一种逻辑的话那就需要改动很多的代码。
所以我们需要抽象出来一种模型来适应相应的变化。
下面我们就来详细了解策略模型在代码中的实践。

2.1 架构图

本文基于的是以下的架构图:
在这里插入图片描述

2.2 策略模式核心

核心入口

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private MyContext myContext;

    @GetMapping("/{value}")
    public String test(@PathVariable("value") String value) {
        return myContext.getInstance(value).handler();
    }


}

通过MyContext去获取到执行的相关信息。

MyContext的源码:

public class MyContext {
    Map<String,Class> map=new HashMap<>();

    public MyContext(Map<String, Class> map) {
        this.map = map;
    }

    public MyHandler getInstance(String type){
        Class clazz  = map.get(type);
        if(StringUtils.isEmpty(clazz)){
            throw new IllegalArgumentException("class of this type is null");
        }
        return (MyHandler) BeanTools.getBean(clazz);
    }
}

核心模型:通过一个map对象存储相关的bean信息,map结构是<注解的type名字,bean的实际class>
然后通过读取map的名字就能获取相关的class

2.3 获取bean

上文说是通过map对象去获取bean的对象的,那map的bean对象是怎么来的呢?
这个的bean的对象是通过实现BeanFactoryPostProcessor(默认spring会扫描这个类的所有对象,执行方法postProcessBeanFactory(spring执行过程中的refreshContext中执行的))

@Component
public class HandlerProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        Map<String,Class> map=new HashMap<>();
        //classScanner是一个工具类,用来扫描所有在包下注解的实现
        ClassScanner classScanner=new DefaultClassScanner();
        classScanner.scanByAnno(Arrays.asList("com.example.demo.Service"), MyHandlerAnnotation.class).forEach(
                aClass -> {
                //获取注解的value值,然后放入map中作为key
                    String value=((MyHandlerAnnotation)aClass.getAnnotation(MyHandlerAnnotation.class)).value();
                    map.put(value,aClass);
                }
        );
        MyContext myContext=new MyContext(map);
        //把myContext注入到spring中,这样上面@AutoWired才能使用
        configurableListableBeanFactory.registerSingleton(myContext.getClass().getName(),myContext);
    }
}

读取bean的方法:(实现BeanFactoryAware就能是spring初始化的时候把beanFactory记录下来)

@Component
public class BeanTools  implements BeanFactoryAware {
    private static BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory=beanFactory;
    }

    public static Object getBean(Class name){
        return beanFactory.getBean(name);
    }
}

2.3 其他代码

注解:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyHandlerAnnotation {
    String value();
}

使用的接口(也可以使用抽象方法):

public interface MyHandler {
    public String handler();
}

使用注解的实现类:

public class FirstHandlerImpl implements MyHandler {

    @Override
    public String handler() {
        return "FirstHandlerImpl";
    }
}

遗留的问题

  1. ClassScanner是一个开源项目还没有阅读人家的源码,只是扫了一眼,感觉是读取目录然后比对;具体还需要继续研究。
  2. ClassScanner和ClassLoader的比较,看看有啥实现的共通和不同。

参考

  1. ClassScan工具类
  2. java通过名称获取实体bean
  3. 在 Spring Boot 中,如何干掉 if else
  4. 策略模式

源码分享 https://gitee.com/eason93/StrategyDemo

策略模式是一种行为型设计模式,它可以用来消除繁琐的if-else语句,并实现算法的动态替换。策略模式使用面向对象的继承和多态机制,使得同一行为在不同场景下具备不同的实现。 在策略模式中,我们将不同的算法封装成不同的策略类,每个策略类都实现了一个共同的接口或基类。客户端根据需要选择使用哪个策略类,从而实现不同的行为。 使用策略模式可以避免代码中大量的if-else语句,提高代码的可读性和可维护性。同时,策略模式也符合开闭原则,可以方便地添加新的策略类。 以下是一个使用策略模式实现if-else的示例: ```python # 定义策略接口 class Strategy: def do_operation(self): pass # 定义具体的策略类 class StrategyA(Strategy): def do_operation(self): print("执行策略A") class StrategyB(Strategy): def do_operation(self): print("执行策略B") class StrategyC(Strategy): def do_operation(self): print("执行策略C") # 定义上下文类 class Context: def __init__(self, strategy): self.strategy = strategy def execute_strategy(self): self.strategy.do_operation() # 客户端代码 strategy_a = StrategyA() strategy_b = StrategyB() strategy_c = StrategyC() context = Context(strategy_a) context.execute_strategy() # 输出:执行策略A context = Context(strategy_b) context.execute_strategy() # 输出:执行策略B context = Context(strategy_c) context.execute_strategy() # 输出:执行策略C ``` 通过使用策略模式,我们可以将不同的行为封装成不同的策略类,客户端根据需要选择使用哪个策略类,从而实现不同的行为。这样就避免了繁琐的if-else语句,使代码更加清晰和可扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值