There are two types of students in a class, Type A: not studying, playing, but playing different things, some playing games, Some are watching TV
Category B: students who are sentry, watching the teacher’s movements and notifying everyone immediately if the teacher enters the class.
This creates a demand. The sentry students should notify all the playing students: the teacher is coming, and different students have different reactions. Some turn off the TV immediately, and some stop playing games.
Observer Pattern: Defines a one-to-many dependency relationship, allowing multiple observer objects to Monitor a topic object.
When this topic object changes state, it will notify all observer objects so that they can automatically update themselves.
Mainly solves:The problem of notifying other objects of status changes of an object, and taking into account ease of use and low coupling to ensure a high degree of collaboration.
When to use:When the state of an object (target object) changes, all dependent objects (observer objects) will be notified and broadcast notifications will be made.
How to solve:Using object-oriented technology, this dependency can be weakened.
Key code:There is an ArrayList in the abstract class to store observers.
/** * 抽象的观察者 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/10 - 15:32 */ public interface Observer { public abstract void updateState(); } /** * 具体的观察者 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/10 - 15:39 */ public class ConcreteObserver implements Observer{ //观察者的姓名 private String name; //观察者的状态 private String observerState; //明确具体的通知者 private ConcreteSubject subject; //get set方法省略 public ConcreteObserver(String name, ConcreteSubject subject) { this.name = name; this.subject = subject; } @Override public void updateState() { observerState=subject.getSubjectState(); System.out.println(name+"在打游戏"); String str=String.format("观察者%s的:新状态是%s", name,observerState); System.out.println(str); } } /** * 具体的观察者 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/10 - 15:39 */ public class ConcreteObserver2 implements Observer{ //观察者的姓名 private String name; //观察者的状态 private String observerState; //明确具体的通知者 private ConcreteSubject subject; //get set方法省略 public ConcreteObserver2(String name, ConcreteSubject subject) { this.name = name; this.subject = subject; } @Override public void updateState() { observerState=subject.getSubjectState(); System.out.println(name+"在看电视"); String str=String.format("观察者%s:新状态是%s", name,observerState); System.out.println(str); } }
/** * 抽象的通知者 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/10 - 15:30 */ public abstract class Subject { //管理观察者的集合 private Listobservers=new ArrayList<>(); //增加观察者 public void add(Observer observer){ observers.add(observer); } //减少观察者 public void detach(Observer observer){ observers.remove(observer); } /** * 通知所有的观察者 */ public void notifyMsg(){ for (Observer observer : observers) { observer.updateState(); } } } /** * 具体的通知者 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/10 - 15:38 */ public class ConcreteSubject extends Subject { //通知者的状态 private String subjectState; //get set方法 public String getSubjectState() { return subjectState; } public void setSubjectState(String subjectState) { this.subjectState = subjectState; } }
/** * 控制台Main方法 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/10 - 15:48 */ public class MainTest { public static void main(String[] args) { //创建一个主题/通知者 ConcreteSubject subject=new ConcreteSubject(); //new出观察者(学生) ConcreteObserver studentZhang = new ConcreteObserver("小张", subject); ConcreteObserver studentLiu = new ConcreteObserver("小刘", subject); ConcreteObserver studentWang = new ConcreteObserver("小王", subject); //将观察者添加到通知队列里 subject.add(studentZhang); subject.add(studentLiu); subject.add(studentWang); //通知者(老师)状态修改,通知每个学生 subject.setSubjectState("老师回来了,我要好好学习"); subject.notifyMsg(); System.out.println("-----------"); } }
Delegation can be regarded as the abstraction of a function and the "class" of a function. The instance of the delegate will represent a specific function
A delegate can carry multiple methods, and all methods are invoked in sequence. The methods carried by the delegate object do not need to be of the same class.
The delegated event model can be defined by three components: events, event sources and event listeners.
The implementation of delegation is simply implemented using reflection.
Implementation
/** * 监听器/观察者 玩游戏 * 事件监听器 * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:17 */ public class PlayingGameListener { public PlayingGameListener(){ System.out.println("我正在玩游戏 开始时间"+new Date()); } public void stopPlayingGame(Date date){ System.out.println("老师来了,快回到座位上,结束时间"+date); } } /** * 监听器/观察者 看电视 * 事件监听器 * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:17 */ public class WatchingTVListener { public WatchingTVListener(){ System.out.println("我正在看电视 "+new Date()); } public void stopWatchingTV(Date date){ System.out.println("老师来了,快关闭电视 。 结束时间"+date); } }
/** * 通知者的抽象类 * 事件源 * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:15 */ public abstract class Notifier { //每个通知者都有一个需要通知的队列(通知:对象、方法、参数) private EventHandler eventHandler=new EventHandler(); public EventHandler getEventHandler() { return eventHandler; } public void setEventHandler(EventHandler eventHandler) { this.eventHandler = eventHandler; } //增加需要帮忙放哨的学生 public abstract void addListener(Object object,String methodName,Object...args); //告诉所有要帮忙放哨的学生:老师来了 public abstract void notifyX(); } /** * 通知者的子类,放哨人 * 事件源 * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:15 */ public class GoodNotifier extends Notifier { @Override public void addListener(Object object, String methodName, Object...args) { System.out.println("有新的同学委托尽职尽责的放哨人!"); this.getEventHandler().addEvent(object, methodName, args); } @Override public void notifyX() { System.out.println("尽职尽责的放哨人告诉所有需要帮忙的同学:老师来了"); try{ //优化:异步通知 this.getEventHandler().notifyX(); }catch(Exception e){ e.printStackTrace(); } } }
/** * 抽象出的事件类,也可以称为方法类 * 事件 * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:03 */ public class Event { //要执行方法的对象 private Object object; //要执行的方法名称 private String methodName; //要执行方法的参数 private Object[] params; //要执行方法的参数类型 private Class[] paramTypes; //若干setter getter public Object getObject() { return object; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public Object[] getParams() { return params; } public void setParams(Object[] params) { this.params = params; } public Class[] getParamTypes() { return paramTypes; } public void setParamTypes(Class[] paramTypes) { this.paramTypes = paramTypes; } public Event(){ } public Event(Object object,String methodName,Object...args){ this.object=object; this.methodName=methodName; this.params=args; contractParamTypes(this.params); } //根据参数数组生成参数类型数组 private void contractParamTypes(Object[] params){ this.paramTypes=new Class[params.length]; for(int i=0;i Copy after login
/** * 管理哪些事件需要执行 * 管理事件 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:03 */ public class EventHandler { //是用一个List private Listobjects; //添加某个对象要执行的事件,及需要的参数 public void addEvent(Object object,String methodName,Object...args){ objects.add(new Event(object,methodName,args)); } public EventHandler(){ objects=new ArrayList (); } //通知所有的对象执行指定的事件 public void notifyX() throws Exception{ for(Event e : objects){ e.invoke(); } } }
Main method
/** * 启动类 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:19 */ public class EventMain { public static void main(String[] args) { //创建一个尽职尽责的放哨者 Notifier goodNotifier = new GoodNotifier(); //创建一个玩游戏的同学,开始玩游戏 PlayingGameListener playingGameListener = new PlayingGameListener(); //创建一个看电视的同学,开始看电视 WatchingTVListener watchingTVListener = new WatchingTVListener(); //玩游戏的同学告诉放哨的同学,老师来了告诉一下 goodNotifier.addListener(playingGameListener, "stopPlayingGame", new Date()); //看电视的同学告诉放哨的同学,老师来了告诉一下 goodNotifier.addListener(watchingTVListener, "stopWatchingTV", new Date()); try { //一点时间后 Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } //老师出现,放哨的人通知所有要帮忙的同学:老师来了 goodNotifier.notifyX(); } }
1. First the observer pattern and then the delegated event technology
2. Observation The observer mode can only notify subclasses that inherit the Observer class, or you can change Observer to an interface
for (Observer observer : observers) { observer.updateState(); }
3. The delegate can notify any method of any class. Reflection, everone
Method method=object.getClass().getMethod(this.getMethodName(), this.getParamTypes()); if(null==method){ return; } method.invoke(this.getObject(), this.getParams());
4. The delegate has one more event executor than the observer, decoupling the observer and the notifier, and can notify any method of any object. Let type A students and type B students be completely decoupled, that is, type A does not know the students of type B at all, but can notify the students of type B
6. Establish a trigger mechanism, you can use asynchronous notification
7. Observer/delegate is quite similar to subscription publishing in MQ. Producers, queues, consumers.
The above is the detailed content of Comparative analysis of examples of observer pattern and delegation in Java. For more information, please follow other related articles on the PHP Chinese website!