在springboot项目中使用策略工厂模式

在springboot项目中使用策略工厂模式

策略接口类

java 复制代码
package cn.test.ext;

public interface ITestStrategy {

    void execTestMethod();
}

策略实现类

java 复制代码
package cn.test.ext.beanlife;

import cn.test.ext.ITestStrategy;
import cn.test.ext.MyStrategyFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * InitializingBean
 * 实现该接口,你的bean可以在 spring容器初始化当前bean时 执行自定义逻辑
 *
 * 必须在afterPropertiesSet方法里写你自定义的执行逻辑
 */
@Slf4j
@Component
public class TestInitializingBean implements ITestStrategy, InitializingBean {

    private static String str ;

    //实现InitializingBean接口,必须重写afterPropertiesSet方法
    //这样当springboot启动加载TestInitializingBean时,会自动执行里边的afterPropertiesSet方法
    @Override
    public void afterPropertiesSet() throws Exception {
        //本方法里可以做一些初始化的操作,如设置类静态变量值 / 将类注册到策略工厂/ 执行一些其他方法或动作/...

        //设置类静态变量值
        str = "qwe123!";

        //将k1策略 (本类) 注册到 策略工厂里
        MyStrategyFactory.register("k1",this);
        log.info("注册了策略:{}到策略工厂里",this);
    }

    @Override
    public void execTestMethod() {
        log.info("start----执行k1策略...");

        System.err.println(str);
    }
}

策略工厂类

java 复制代码
package cn.test.ext;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略工厂
 */
@Component
@Slf4j
public class MyStrategyFactory {

    private static Map<String, ITestStrategy> strategyMap = new ConcurrentHashMap<>();


    /**
     * 将每个策略注册到策略工厂
     *
     * 利用策略类里的 InitializingBean 的afterPropertiesSet方法,
     * 来把策略类初始化到StrategyFactory 策略工厂里
     *
     * @param strategyCode String
     * @param itemStrategy IPersonAddrSimilarComputeHandleStrategy
     */
    public static void register(String strategyCode, ITestStrategy itemStrategy) {
        strategyMap.put(strategyCode, itemStrategy);
    }

    /**
     * 从策略工厂取出需要的策略
     * @param key
     * @return
     */
    public static ITestStrategy getReceiptHandleStrategy(String key) {
        for (String strate : strategyMap.keySet()) {
            if (StrUtil.equals(strate, key)) {
                //按key取出对应的策略类,来执行处理策略
                ITestStrategy strategy = strategyMap.get(key);
                log.info("拿到了策略:{}", strategy.getClass().getName());
                return strategy;
            }
        }
        log.info("没拿到对应的策略类,不存在 策略:{}", key);
        return null;
    }
}

策略上下文类

java 复制代码
package cn.test.ext;

import org.springframework.stereotype.Component;

@Component
public class MyStrategyContext {

    private ITestStrategy iTestStrategy;

    //1.按key get某个策略类
    public void getStrategyInstance(String key) {
        ITestStrategy strategyInstance = MyStrategyFactory.getReceiptHandleStrategy(key);
        this.iTestStrategy = strategyInstance;
    }

    //2.通过context执行该策略类里的方法
    public void execMethod(){
        if (iTestStrategy!=null){
            iTestStrategy.execTestMethod();
        }
    }
}
相关推荐
哪吒编程2 分钟前
写出高性能Java代码,synchronized使用的三个关键问题全解析
java·后端
王者鳜錸3 分钟前
三、小白学JAVA-比较运算符与循环
java·开发语言·算法
neoooo7 分钟前
打造一个 Spring Boot Starter:实现字符串工具库并探讨其价值
java·spring boot·spring
三体世界16 分钟前
C++ STL序列式容器之一 string
java·c语言·开发语言·c++·windows·visual studio·string
lynn-6628 分钟前
JAVA使用opencv实现人脸识别
java·开发语言·python
Stimd34 分钟前
【重写SpringFramework】条件判定(chapter 4-4)
java·后端·spring
TFHoney43 分钟前
Java面试第十三山!《设计模式》
java·设计模式·面试
shan~~1 小时前
springboot完成复制一个word内容包括格式到另外一个word
spring boot·后端·word
qq_339282232 小时前
Tomcat升级步骤
java·tomcat