Java design patterns - event-driven mode (observer pattern)
Roles
- event
- Event Source
- Event Listeners
event
General event classes inherit from java.util.EventObject
, a package of event source and event-related information with
source
: Event Source getSource
(): Gets the event source
public class EventObject implements java.io.Serializable {
private static final long serialVersionUID = 5516075349620653480L;
/**
* The object on which the Event initially occurred
*/
protected transient Object source;
/**
* Constructs a prototypical Event.
*
* @param source The object on which the Event initially occurred.
* @exception IllegalArgumentException if source is null.
*/
public EventObject(Object source) {
if (source == null)
throw new IllegalArgumentException("null source");
this.source = source;
}
/**
* The object on which the Event initially occurred.
*
* @return The object on which the Event initially occurred.
*/
public Object getSource() {
return source;
}
/**
* Returns a String representation of this EventObject.
*
* @return A a String representation of this EventObject.
*/
public String toString() {
return getClass().getName() + "[source=" + source + "]";
}
}
Event Source
Each event includes an event source, the event source is the local events, properties due to an event or state change of the source, will generate the corresponding event object, the event object then notifies the event listener to all listeners source .
Event Listeners
General need to implement event listenersjava.util.EventListener接口
public interface EventListener {
}
EventListener
Is an empty interface, the listener must have a callback method for the event source callback, the callback method can be inherited or implement EventListener
the interface when the custom.
Achieving source
-
Event listeners: an event listener can directly implement the
EventListener
interface, but the general framework will have its own listener interfaces so first inherited hereEventListener
Interfacepublic interface ApplicationListener extends EventListener{ void onApplicationEvent(ApplicationListener event); }
-
Event: Event classes can be used directly
EventObject
, here through inheritanceEventObject
to achieve their events Classespublic class ApplicationEvent extends EventObject { /** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @throws IllegalArgumentException if source is null. */ public ApplicationEvent(Object source) { super(source); } }
-
Operational context
public class ApplicationContext { /** * 存放所有的监听器 */ Set<ApplicationListener> listeners; public ApplicationContext() { this.listeners = new HashSet<>(); } /** * 添加监听器 * @param listener 监听器 */ public void addApplicationListener(ApplicationListener listener) { this.listeners.add(listener); } /** * 发布事件 * 回调所有监听器的回调方法 * @param event 事件 */ public void publishEvent(ApplicationEvent event) { for (ApplicationListener listener : listeners) { listener.onApplicationEvent(event); } } }
-
Test category
public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext = new ApplicationContext(); /** * 添加监听事件源为整型的监听器 */ applicationContext.addApplicationListener(event -> { Object source = event.getSource(); if (source instanceof Integer) { int now = (int) source; System.out.println("检测到事件源为整型:事件源变为" + now); } }); /** * 添加监听事件源为字符串类型的监听器 */ applicationContext.addApplicationListener(event -> { Object source = event.getSource(); if (source instanceof String) { String now = (String) source; System.out.println("检测到事件源为字符串类型:事件源变为" + now); } }); /** * 发布事件 */ // applicationContext.publishEvent(new ApplicationEvent(1001)); applicationContext.publishEvent(new ApplicationEvent("Hello")); } }
-
Export
检测到事件源为字符串类型:事件源变为Hello
Spring Event-driven
spring event-driven and above implementation is somewhat different, the biggest difference is the addition of the way listener, spring is generally added way to achieve ApplicationListener
lower automatically scan path interface, then the implementation class is registered as Bean, spring context initialization time implementation of ApplicationListener
the interface class, and then add the listener to the collection inside, when the release of events will notify all listeners, it all depends spring of Bean container management functions.
Author: vczyh
link: https: //www.jianshu.com/p/724e5814f78b
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.