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

}
相关推荐
Pasregret4 小时前
策略模式:动态切换算法的设计智慧
算法·bash·策略模式
Leaf吧2 天前
java 设计模式 策略模式
java·设计模式·策略模式
knowledgebao2 天前
osxcross 搭建 macOS 交叉编译环境
macos·策略模式
〆、风神7 天前
Spring Boot实战:基于策略模式+代理模式手写幂等性注解组件
spring boot·代理模式·策略模式
邪恶的贝利亚8 天前
设计模式实践:模板方法、观察者与策略模式详解
设计模式·策略模式
进击的圆儿8 天前
策略模式简单介绍
策略模式
死也不注释8 天前
【设计模式——策略模式】
设计模式·策略模式
未定义.22111 天前
Java设计模式实战:策略模式在SimUDuck问题中的应用
java·设计模式·策略模式
爱叨叨的程序狗11 天前
策略模式随笔~
策略模式
我只有一岁半12 天前
策略模式实现 Bean 注入时怎么知道具体注入的是哪个 Bean?
spring·策略模式