1、策略+工厂模式
定义策略接口,不同的策略都实现这个接口:
public interface HandleSetStatusService {
public void handleMessage(WebSocketSession session, ImClient imClient,
long sellerId, WsRecvWrapper request);
}
/**
* 策略实现类,
* 咨询师发送消息
*/
@Service
@HandleServiceStrategyAspect(ImConstants.SELLER_MSG_SEND)
public class HandleSellerMsgSendServiceImpl implements HandleSetStatusService {
@Autowired
private ConsultSessionService consultSessionService;
@Override
public void handleMessage(WebSocketSession session, ImClient imClient,
long sellerId, WsRecvWrapper request) {
XXX
}
定义注解,用来区分不同的策略实现
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface HandleServiceStrategyAspect {
String value();
}
@Component
public class MessageServiceListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(HandleServiceStrategyAspect.class);
HandleServiceStrategyFactory handleServiceStrategyFactory = event.getApplicationContext().getBean(HandleServiceStrategyFactory.class);
beans.forEach((name, bean) -> {
HandleServiceStrategyAspect typeHandler = bean.getClass().getAnnotation(HandleServiceStrategyAspect.class);
handleServiceStrategyFactory.register(typeHandler.value(), (HandleSetStatusSe