使用模板方法处理枚举事件的心得
1、定义枚举类型
一般情况我们有多重事件类型的情况肯定先自定义一个事件类型的枚举,代码示例如下:
@Getter
public enum EventTypeEnum {
TYPE1(1,"类型1"),
TYPE2(2, "类型2"),
TYPE3(3, "类型3"),
;
private int value;//vn
private String name;//kn
public static EventTypeEnum valueOf(int value) {
for(EventTypeEnum item : EventTypeEnum.values()){
if(item.value == value){
return item;
}
}
return null;
}
private EventTypeEnum(Integer type, String typeName){
this.value = type;
this.name = typeName;
}
}
2、通常的handler 处理
很自然的我们想到了使用switch来操作,一般switch和枚举是绝配,如下代码展示:
public class EventHandler {
private void handler(List<DefineEvent> eventList) {
if (CollectionUtils.isEmpty(eventList)) {