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

}
相关推荐
Ronin3052 天前
【Linux系统】日志与策略模式
linux·策略模式·日志
北执南念3 天前
基于 Spring 的策略模式框架,用于根据不同的类的标识获取对应的处理器实例
java·spring·策略模式
dyxal4 天前
使用tree命令导出文件夹/文件的目录树( Windows 和 macOS)
windows·macos·策略模式
酷炫码神4 天前
第 2 篇:Java 入门实战(JDK8 版)—— 编写第一个 Java 程序,理解基础运行逻辑
java·开发语言·策略模式
程序员TNT5 天前
Shoptnt 促销计算引擎详解:策略模式与责任链的完美融合
linux·windows·策略模式
new_daimond6 天前
设计模式-策略模式深度分析
设计模式·策略模式
织_网6 天前
Electron 核心模块速查表
javascript·electron·策略模式
至此流年莫相忘7 天前
设计模式:策略模式
设计模式·策略模式
特种加菲猫8 天前
并发编程的守护者:信号量与日志策略模式解析
linux·笔记·策略模式
xiaowu0809 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式