Code background
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
Introduction
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.
Implementation
Observer (student)
/** * 抽象的观察者 * * @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); } }
Notifier (teacher)
/** * 抽象的通知者 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/10 - 15:30 */ public abstract class Subject { //管理观察者的集合 private List<Observer> observers=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 method
/** * 控制台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 introduction
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
Observer
/** * 监听器/观察者 玩游戏 * 事件监听器 * @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); } }
Notifier
/** * 通知者的抽象类 * 事件源 * @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(); } } }
Event
/** * 抽象出的事件类,也可以称为方法类 * 事件 * @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<params.length;i++){ this.paramTypes[i]=params[i].getClass(); } } //执行该 对象的该方法 public void invoke() throws Exception{ //通过class,method,paramTypes 确定执行哪个类的哪个方法 Method method=object.getClass().getMethod(this.getMethodName(), this.getParamTypes()); if(null==method){ return; } //方法执行 method.invoke(this.getObject(), this.getParams()); } }
Event handling
/** * 管理哪些事件需要执行 * 管理事件 * * @author Promsing(张有博) * @version 1.0.0 * @since 2022/5/8 - 11:03 */ public class EventHandler { //是用一个List private List<Event> objects; //添加某个对象要执行的事件,及需要的参数 public void addEvent(Object object,String methodName,Object...args){ objects.add(new Event(object,methodName,args)); } public EventHandler(){ objects=new ArrayList<Event>(); } //通知所有的对象执行指定的事件 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(); } }
Summary
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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
