Table of Contents
Code background
Observer Pattern
Introduction
Implementation
Observer (student)
Notifier (teacher)
Main method
Delegation introduction
Observer
Notifier
Event
Event handling
Summary
Home Java javaTutorial Comparative analysis of examples of observer pattern and delegation in Java

Comparative analysis of examples of observer pattern and delegation in Java

May 08, 2023 pm 04:37 PM
java

    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

    Comparative analysis of examples of observer pattern and delegation in Java

    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);
        }
    }
    Copy after login
    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;
        }
    }
    Copy after login
    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("-----------");
        }
    }
    Copy after login

    Comparative analysis of examples of observer pattern and delegation in Java

    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

    Comparative analysis of examples of observer pattern and delegation in Java

    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);
        }
    }
    Copy after login
    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();
            }
        }
    }
    Copy after login
    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());
        }
    }
    Copy after login
    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();
            }
        }
    }
    Copy after login

    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();
        }
    }
    Copy after login

    Comparative analysis of examples of observer pattern and delegation in Java

    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();
    }
    Copy after login

    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());
    Copy after login

    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!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

    Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

    Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

    Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

    Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

    Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

    Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

    Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

    Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

    In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

    Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

    Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

    TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

    Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

    Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

    Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

    See all articles