1.监听器基于观察者模式。
1.myTestEvent自定义事件。被观察者
public class MyTestEvent extends ApplicationEvent
2.MyNoAnnotationListener 自定义监听器。观察者
@Component
public class MyNoAnnotationListener implements ApplicationListener<MyTestEvent> {
@Override
public void onApplicationEvent(MyTestEvent event) {
System.out.println("非注解监听器:" + event.getMsg());
}
}
3.发布自定义事件。
@Component
public class MyTestEventPubLisher {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
/**
* 事件发布方法
*/
public void pushListener(String msg) {
applicationEventPublisher.publishEvent(new MyTestEvent(this, msg));
}
}
2.把if else拆开,走同一个接口的不同实现,策略模式。
3.构建者模式
4.卖咖啡装饰器模式。
5.log日志适配器模式。