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"));
    }

}
相关推荐
太过平凡的小蚂蚁4 天前
策略模式:让算法选择像点菜一样简单
算法·策略模式
让我上个超影吧5 天前
设计模式【工厂模式和策略模式】
java·设计模式·策略模式
bkspiderx8 天前
C++设计模式之行为型模式:策略模式(Strategy)
c++·设计模式·策略模式
_Yoke10 天前
Java 枚举多态在系统中的实战演进:从枚举策略到自动注册
java·springboot·策略模式
笨手笨脚の11 天前
设计模式-策略模式
设计模式·策略模式·行为型设计模式
2501_9159214311 天前
iOS 26 崩溃日志解析,新版系统下崩溃获取与诊断策略
android·ios·小程序·uni-app·cocoa·iphone·策略模式
liulilittle16 天前
macOS 内核路由表操作:直接 API 编程指南
网络·c++·macos·策略模式·路由·route·通信
青草地溪水旁17 天前
设计模式(C++)详解——策略模式(1)
c++·设计模式·策略模式
o0向阳而生0o17 天前
105、23种设计模式之策略模式(14/23)
设计模式·策略模式
青草地溪水旁17 天前
设计模式(C++)详解——策略模式(2)
c++·设计模式·策略模式