一,介绍
先上案例
if (msgType = "文本") {
// dosomething
} else if(msgType = "图片") {
// doshomething
} else if(msgType = "视频") {
// doshomething
} else {
// doshomething
}
随着代码的业务逻辑有时候会伴随着很多的 if-else 判断 导致代码严重 的冗余,而且很不美观。可以根据不同的类型有不同的处理策略,进行代码重构,使代码简洁美观,显得更加的高大上
转载借阅资料地址:
1.项目中的if else太多了,该怎么重构?
2.利用策略模式优化过多 if else 代码
二,具体案例代码
1,对象
用来进行 判断类型 的存储
@Data
@AllArgsConstructor
public class MessageInfo {
// 类型
private Integer type;
// 内容
private String content;
}
2,枚举类
用来获取不同编码,对应的不同种类别
/**
* @program: wangmx
* @Date: 2019/11/28 16:03
* @Author: wangmx
* @Description: 枚举
*/
public enum MSG_TYPE {
TEXT(1, "文本"),
IMAGE(2, "图片"),
VIDEO(3, "视频");
public final int code;
public final String name;
MSG_TYPE(int code, String name) {
this.code = code;
this.name = name;
}
}
3,自定义注解
用来 类 区分是那种 类型
/**
* @program: wangmx
* @Date: 2019/11/28 16:10
* @Author: wangmx
* @Description:
*/
@Documented
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MsgTypeHandler {
MSG_TYPE value();
}
4,处理接口
自定义一个处理 类型 的接口
/**
* @program: wangmx
* @Date: 2019/11/28 16:00
* @Author: wangmx
* @Description:
*/
public interface MessageService {
void handleMessage(MessageInfo messageInfo);
}
5,处理接口的实现
三个不同的实现,用来分别处理不同种类的 的业务代码,重点:@MsgTypeHandler(value = MSG_TYPE.IMAGE)
用来区分 不同类别的 接口实现
/**
* @program: wangmx
* @Date: 2019/11/28 16:19
* @Author: wangmx
* @Description:
*/
@Service
@MsgTypeHandler(value = MSG_TYPE.IMAGE)
public class ImageMessageService implements MessageService {
@Override
public void handleMessage(MessageInfo messageInfo) {
System.out.println("处理图片消息 " + messageInfo.getContent());
}
}
/**
* @program: wangmx
* @Date: 2019/11/28 16:00
* @Author: wangmx
* @Description:
*/
@Service
@MsgTypeHandler(value = MSG_TYPE.TEXT)
public class TextMessageService implements MessageService {
@Override
public void handleMessage(MessageInfo messageInfo) {
System.out.println("处理文本消息 " + messageInfo.getContent());
}
}
/**
* @program: wangmx
* @Date: 2019/11/28 16:00
* @Author: wangmx
* @Description:
*/
@Service
@MsgTypeHandler(value = MSG_TYPE.VIDEO)
public class VideoMessageService implements MessageService {
@Override
public void handleMessage(MessageInfo messageInfo) {
System.out.println("处理文本消息 " + messageInfo.getContent());
}
}
6,保存类型 --> 处理对象的映射关系
/**
* @program: wangmx
* @Date: 2019/11/28 16:12
* @Author: wangmx
* @Description:
*/
@Component
public class MessageServiceContext {
private final Map<Integer, MessageService> handlerMap = new HashMap<>();
public MessageService getMessageService(Integer type) {
return handlerMap.get(type);
}
public void putMessageService(Integer code, MessageService messageService) {
handlerMap.put(code, messageService);
}
}
7,处理不同的映射关系 存储 对应的 实现接口
/**
* @program: wangmx
* @Date: 2019/11/28 16:13
* @Author: wangmx
* @Description:
*/
@Component
public class MessageServiceListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(MsgTypeHandler.class);
MessageServiceContext messageServiceContext = event.getApplicationContext().getBean(MessageServiceContext.class);
beans.forEach((name, bean) -> {
MsgTypeHandler typeHandler = bean.getClass().getAnnotation(MsgTypeHandler.class);
messageServiceContext.putMessageService(typeHandler.value().code, (MessageService) bean);
});
}
}
三,测试
/**
* @program: wangmx
* @Date: 2019/11/28 16:13
* @Author: wangmx
* @Description:
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class testMassage {
@Autowired
MessageServiceContext messageServiceContext;
@Test
public void contextLoads() {
// 构建一个文本消息
MessageInfo messageInfo = new MessageInfo(MSG_TYPE.TEXT.code, "枚举类型");
MessageService messageService = messageServiceContext.getMessageService(messageInfo.getType());
// 处理文本消息 消息内容
// 可以看到文本消息被文本处理类所处理
messageService.handleMessage(messageInfo);
}
}