spring 枚举、策略模式、InitializingBean初使化组合使用示例

实现一个简单的文本处理系统。

在这个系统中,我们将定义不同类型的文本处理策略,比如大小写转换、添加前缀后缀等,并使用工厂模式来管理这些策略。
1 定义一个枚举来标识不同的文本处理类型

复制代码
public enum TextProcessTypeEnum {
    UPPER_CASE,
    LOWER_CASE,
    PREFIX_SUFFIX
    // 可以继续添加更多的文本处理类型
}

2 定义一个策略接口,所有的文本处理策略都将实现这个接口

复制代码
public interface TextProcessHandler {

    public String process(String txt);

    public TextProcessTypeEnum getHandlerType();
}

3 实现几个具体的策略类

复制代码
import org.springframework.stereotype.Service;

@Service
public class UpperCaseHandler implements TextProcessHandler {
    @Override
    public String process(String txt) {
        return txt.toUpperCase();
    }

    @Override
    public TextProcessTypeEnum getHandlerType() {
        return TextProcessTypeEnum.UPPER_CASE;
    }
}



@Service
public class LowerCaseHandler implements TextProcessHandler {
    @Override
    public String process(String txt) {
        return txt.toLowerCase();
    }

    @Override
    public TextProcessTypeEnum getHandlerType() {
        return TextProcessTypeEnum.LOWER_CASE;
    }
}



@Service
@NoArgsConstructor
public class PrefixSuffixHandler implements TextProcessHandler {

    @Value("start--")
    private String prefix;

    @Value("--end")
    private String suffix;

    public PrefixSuffixHandler(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = suffix;
    }

    @Override
    public String process(String txt) {
        return prefix +txt+ suffix;
    }

    @Override
    public TextProcessTypeEnum getHandlerType() {
        return TextProcessTypeEnum.PREFIX_SUFFIX;
    }
}

4 创建一个工厂类来管理这些策略

复制代码
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TextProcessHandlerFactory implements InitializingBean {

    @Autowired
    private List<TextProcessHandler> textProcessHandlerList;

    private final Map<TextProcessTypeEnum, TextProcessHandler> handlerMap =new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {

        System.out.println("textProcessHandlerList = " + textProcessHandlerList);

        for (TextProcessHandler handler : textProcessHandlerList) {
            handlerMap.put(handler.getHandlerType(), handler);
        }

        System.out.println("handlerMap = " + handlerMap);

    }

    public TextProcessHandler getHandler(TextProcessTypeEnum typeEnum)
    {
        return handlerMap.get(typeEnum);
    }
}

5 使用这个工厂来获取相应的处理器,并处理文本

复制代码
@SpringBootTest
class DemoMvnTest1ApplicationTests {

    @Autowired
    private TextProcessHandlerFactory factory;

    @Test
    void contextLoads() {
        TextProcessHandler handler = factory.getHandler(TextProcessTypeEnum.UPPER_CASE);
        System.out.println(handler.process("hello world"));

        System.out.println();

        handler = factory.getHandler(TextProcessTypeEnum.LOWER_CASE);
        System.out.println(handler.process("HELLO WORLD"));

        System.out.println();

        handler = factory.getHandler(TextProcessTypeEnum.PREFIX_SUFFIX);
        System.out.println(handler.process("hello world"));
    }

}
相关推荐
xiaowu0807 小时前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
彭于晏Yan18 小时前
Spring Boot中策略模式结合依赖注入的实现方式
spring boot·策略模式
宁静致远202118 小时前
【C++设计模式】第二篇:策略模式(Strategy)--从基本介绍,内部原理、应用场景、使用方法,常见问题和解决方案进行深度解析
c++·设计模式·策略模式
Meteors.2 天前
23种设计模式——策略模式 (Strategy Pattern)详解
设计模式·策略模式
qq_435139572 天前
【1】策略模式 + 模板方法模式的联合应用
策略模式·模板方法模式
谢栋_3 天前
设计模式从入门到精通之(六)策略模式
设计模式·bash·策略模式
Nerd Nirvana3 天前
C++编程——异步处理、事件驱动编程和策略模式
开发语言·c++·策略模式·嵌入式开发·事件驱动·异步处理
成都被卷死的程序员4 天前
策略模式 + 工厂模式
策略模式
oioihoii7 天前
VS Code C#调试完全指南
开发语言·c#·策略模式
郝学胜-神的一滴7 天前
策略模式:模拟八路军的抗日策略
开发语言·c++·程序人生·设计模式·策略模式