惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - npe0

博文阅读密码验证 - 博客园 MyBatis-Plus 的动态SQL片段用法 EasyExcel的多级表头 docker镜像搬运命令 macbook绕过安全控制安装第三方软件 Python中使用列表、map和filter函数配合lambda表达式来操作集合 阿里通义千问大模型初探 java.io.IOException: Cannot run program “az”: CreateProcess error=2, 系统找不到指定的文件。 Git使用问题记录 seata的分布式事务处理机制 JVM常用命令 你可能不知道的Collectors用法 easyexcel导出Bigdecimal数据格式问题 百万千万级Excel导出 arthas 常用命令 对find,xargs,grep和管道的一些深入理解 PlantUML 分库分表 jvm优化
策略模式在项目中的应用
npe0 · 2026-04-13 · via 博客园 - npe0

一、定义事件类型枚举

package org.example.cfo.handler;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.stream.Stream;

/**
*事件类型枚举
*/
@Getter

public enum LolEventTypeEnum {
    /**
     * 完成
     */
    FINISHED("完成", "FINISHED"),

/**

 * 开始
   */
   START("开始", "START"),

/**

 * 执行
   */
   EXECUTED("执行", "EXECUTED");

private final String label;
private final String value;

LolEventTypeEnum(String label, String value) {
    this.label = label;
    this.value = value;
}

public static LolEventTypeEnum getByValue(String value) {
    return Stream.of(values()).filter(var -> StringUtils.equalsIgnoreCase(var.getValue(), value))
            .findFirst()
            .orElse(null);

}
}

二、定义事件处理接口、基类、处理器工厂

2.1、接口

package org.example.cfo.handler;

/**

*事件处理器接口
*/
public interface LolEventHandler {

/**

*@param lolMessage 消息
*/
void handle(LolMessage lolMessage);

/**

*获取支持的事件类型枚举
*

*@return 事件类型枚举
*/
LolEventTypeEnum getSupportedEventType();
}

2.2、基类

package org.example.cfo.handler;

import lombok.extern.slf4j.Slf4j;

/**

*抽象类不能被实例化:Spring 无法直接创建抽象类的实例

*依赖注入是通过子类实现的:当子类被 Spring 管理时,它会继承父类的 @Resource 注解的依赖

*Spring 会自动处理继承的依赖注入:子类实例化时,会自动注入父类中声明的依赖
*/
@Slf4j
public abstract class AbstractLolEventHandler implements LolEventHandler {

//@Resource
//protected LolMapper lolMapper;

// protected 子类的共用方法1

// protected 子类的共用方法2


}

2.3、处理器工厂

package org.example.cfo.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

*事件处理器工厂
*/
@Slf4j
@Component
public class LolEventHandlerFactory {

private final List<LolEventHandler> eventHandlers;

private final Map<String, LolEventHandler> handlerMap = new HashMap<>();

public LolEventHandlerFactory(List<LolEventHandler> eventHandlers) {
    this.eventHandlers = eventHandlers;
}

/**

*初始化处理器映射
*/
@PostConstruct
public void init() {
for (LolEventHandler handler : eventHandlers) {
    LolEventTypeEnum eventTypeEnum = handler.getSupportedEventType();
    String eventTypeValue = eventTypeEnum.getValue();
    handlerMap.put(eventTypeValue, handler);
    log.info("事件处理器 - eventType: {}, label: {}, handler: {}",
            eventTypeValue, eventTypeEnum.getLabel(), handler.getClass().getSimpleName());
}
log.info("事件处理器工厂初始化完成,共注册 {} 个处理器", handlerMap.size());
}

/**

*根据 eventType 获取对应的处理器
*

*@param eventType 事件类型

*@return 对应的处理器,如果不存在则返回 null
*/
public LolEventHandler getHandler(String eventType) {
return handlerMap.get(eventType);
}

/**

*根据 eventType 枚举获取对应的处理器
*

*@param eventTypeEnum 事件类型枚举

*@return 对应的处理器,如果不存在则返回 null
*/
public LolEventHandler getHandler(LolEventTypeEnum eventTypeEnum) {
return handlerMap.get(eventTypeEnum.getValue());
}

/**

*判断是否存在指定 eventType 的处理器
*

*@param eventType 事件类型

*@return 是否存在
*/
public boolean hasHandler(String eventType) {
return handlerMap.containsKey(eventType);
}
}

三、定义数据Body

package org.example.cfo.handler;

import lombok.Data;

import java.io.Serializable;

/**

*执行事件
*/
@Data
public class LolMessage implements Serializable {

private static final long serialVersionUID = 1L;

/**

事件类型
*/
private String eventType;

/**

*消息ID
*/
private Long messageId;

/**

*事件时间(时间戳)
*/
private Long eventTime;

/**

*操作人
*/
private String operator;
}

四、子类定义

package org.example.cfo.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**

*确认执行事件处理器
*/
@Slf4j
@Component
public class StartEventHandler extends AbstractLolEventHandler {

@Override
public void handle(LolMessage lolMessage) {
    log.info("ATA消费消息-确认执行-事件 - processInstanceId: {}", lolMessage.getMessageId());

}

@Override
public LolEventTypeEnum getSupportedEventType() {
    return LolEventTypeEnum.START;
}
}



package org.example.cfo.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**

*执行事件处理器
*/
@Slf4j
@Component
public class ExecutedEventHandler extends AbstractLolEventHandler {

@Override
public void handle(LolMessage lolMessage) {
    log.info("ATA消费消息-退回执行-事件 - processInstanceId: {}", lolMessage.getMessageId());


    }
    
    @Override
    public LolEventTypeEnum getSupportedEventType() {
        return LolEventTypeEnum.EXECUTED;
    }

}


package org.example.cfo.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**

*完成事件处理器
*/
@Slf4j
@Component
public class FinishedEventHandler extends AbstractLolEventHandler {

@Override
public void handle(LolMessage lolMessage) {
   log.info("消费消息-完成事件-处理流程 - processInstanceId: {}", lolMessage.getMessageId());

}

@Override
public LolEventTypeEnum getSupportedEventType() {
   return LolEventTypeEnum.FINISHED;
}
}