策略模式加上InitializingBean的编程实现

Posted smileNicky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了策略模式加上InitializingBean的编程实现相关的知识,希望对你有一定的参考价值。

场景:最近在写一些接口,发现堆了太多的if…eles,所以通过实现Spring框架的InitializingBean接口,加上策略模式,避免代码里堆了太多if…else

实现思路:

  • 实现 ApplicationContextAware 接口,通过该接口获取 Spring 容器
  • 实现 InitializingBean 接口,接口中定义的 afterPropertiesSet(),Spring框架进行属性填充之后,将策略类加载到一个list列表里
  • 通过对应的标识flag获取对应的策略类,通过策略类调用具体方法

大量的if…else堆积:

if (condition1 || condition2) 
	// 具体实现业务
 else if ( condition3 || condition4) 
	// 具体业务
 else if (condition5 || condition6) 
	// ...
 
... 

策略类接口

public interface InfoStrategy 
    List<InfoModel> obtainInfo(InfoQeury qeury);
    String getType();


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;


@Service
@Slf4j
public class InfoServiceImpl implements InfoService, ApplicationContextAware, InitializingBean 

    private List<LInfoStrategy> queryStrategys = new ArrayList<>();
    private ApplicationContext applicationContext;

    @Override
    public List<InfoModel> listInfo(InfoQeury query) 
        String type = query.getType();
        log.info("传参 Type :  , Code : " , type , code);
        return getStrategy(type).obtainInfo(query);
    

    @Component
    class Strategy1 implements InfoStrategy 
        @Override
        public List<InfoModel> obtainInfo(InfoQeury qeury) 
            // ...
            return infos;
        
        @Override
        public String getType() 
            return FlagType.1_FLAG.getCode();
        
    

    @Component
    class Strategy2 implements InfoStrategy 
        @Override
        public List<InfoModel> obtainInfo(InfoQeury qeury) 
            // ...
            return infos;
        
        @Override
        public String getType() 
            return FlagType.2_FLAG.getCode();
        
    

    @Component
    class Strategy3 implements InfoStrategy 
        @Override
        public List<InfoModel> obtainInfo(InfoQeury qeury) 
            // ...
            return infos;
        
        @Override
        public String getType() 
            return FlagType.3_FLAG.getCode();
        
    


    private InfoStrategy getStrategy(String strategyName) 
        for (InfoStrategy strategy : queryStrategys) 
            if (strategy.getType().equals(strategyName)) 
                return strategy;
            
        
        return null;
    

    @Override
    public void afterPropertiesSet() throws Exception 
        Collection<InfoStrategy> strategys =
                applicationContext.getBeansOfType(InfoStrategy.class).values();
        queryStrategys.addAll(strategys);
    

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException 
        this.applicationContext = applicationContext;
    




以上是关于策略模式加上InitializingBean的编程实现的主要内容,如果未能解决你的问题,请参考以下文章

Spring利用InitializingBean定义基于业务队列处理Handler

函数式编程中的策略模式

内部类与函数式编程

编程学习之简单工厂模式与策略模式

面向对象编程思想-策略模式

从头认识设计模式-策略模式-05-引入设计原则:面向接口编程