一、监听器设计模式
监听器的实现方案是观察者模式实现的一种,另外也称之为发布-订阅模式。观察者模式
1、监听器模式介绍
2、系统监听器介绍
3、监听事件触发机制
4、自定义监听器实践
1、监听器模式介绍
监听模式要素
事件
监听器
广播器
触发机制
2、监听器模式实践
模拟场景: 天气是下雨下雪,则通过广播器广播这些事件,如果对下雨下雪感兴趣的监听器,就监听这些事件,做出相应的行为
1) 事件
public abstract class WeatherEvent { public abstract String getWeather(); }
下雨事件
public class RainEvent extends WeatherEvent{ @Override public String getWeather() { return "rain"; } }
下雪事件
public class SnowEvent extends WeatherEvent{ @Override public String getWeather() { return "snow"; } }
2)监听器
public interface WeatherListener { void onWeatherEvent(WeatherEvent event); }
下雨监听器
/** * 下雨监听器 */ public class RainListener implements WeatherListener { @Override public void onWeatherEvent(WeatherEvent event) { if(event instanceof RainEvent){ System.out.println("hello " +((RainEvent) event).getWeather()); } } }
下雪监听器
/** * 下雪监听器 */ public class SnowListener implements WeatherListener { @Override public void onWeatherEvent(WeatherEvent event) { if(event instanceof SnowEvent){ System.out.println("hello " +((SnowEvent) event).getWeather()); } } }
3)广播器
事件广播接口
/** * 广播器 */ public interface EventMulticaster { //广播事件 void multicastEvent(WeatherEvent event); void addListener(WeatherListener weatherListener); void removeListener(WeatherListener weatherListener); }
抽象事件广播器
public abstract class AbstractEventMulticaster implements EventMulticaster { private List<WeatherListener> listenerList = new ArrayList<>(); @Override public void multicastEvent(WeatherEvent event) { doStart(); listenerList.forEach(i -> i.onWeatherEvent(event)); doEnd(); } protected abstract void doStart(); protected abstract void doEnd(); @Override public void addListener(WeatherListener weatherListener) { listenerList.add(weatherListener); } @Override public void removeListener(WeatherListener weatherListener) { listenerList.remove(weatherListener); } }
天气广播器
public class WeatherEventMulticaster extends AbstractEventMulticaster{ @Override protected void doStart() { System.out.println("begin boradcast weacher event"); } @Override protected void doEnd() { System.out.println("end boradcast weacher event"); } }
4)触发机制
public class Test { public static void main(String[] args) { //构造一个广播器 WeatherEventMulticaster eventMulticaster = new WeatherEventMulticaster(); //创建两个监听器 RainListener rainListener = new RainListener(); SnowListener snowListener = new SnowListener(); //将监听器添加到广播器当中 eventMulticaster.addListener(rainListener); eventMulticaster.addListener(snowListener); //广播器广播下雪事件 eventMulticaster.multicastEvent(new SnowEvent()); //广播器广播下雨事件 eventMulticaster.multicastEvent(new RainEvent()); System.out.println("--------------------"); eventMulticaster.removeListener(rainListener); //广播器广播下雪事件 eventMulticaster.multicastEvent(new SnowEvent()); //广播器广播下雨事件 eventMulticaster.multicastEvent(new RainEvent()); } }
打印结果
begin boradcast weacher event hello snow end boradcast weacher event begin boradcast weacher event hello rain end boradcast weacher event -------------------- begin boradcast weacher event hello snow end boradcast weacher event begin boradcast weacher event end boradcast weacher event
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!