zoukankan      html  css  js  c++  java
  • 观察者模式

    观察者模式

    1.基础知识:

    定义:

    定义了对象之间的一对多依赖,让多个观察者对象同时监听某

    一个主题对象,当主题对象发生变化时,它的所有依赖者(观察者)都会收到通知并更新

    现实中的应用:

    微信朋友圈点赞,商品降价提醒,监听器的概念

    使用场景:

    关联行为场景,建立一套触发机制

    优点:

    观察者和被观察者之间建立一个抽象的耦合

    观察者模式支持广播通信

    缺点:

    观察者之间有过多的细节依赖、提高时间消耗及程序复杂度

    使用要得当,要避免循环调用


    2.举例

    课程类

    /**
     * 课程类
     * 被观察对象
     */
    public class Course extends Observable {
        private String courseName;
        public Course(String courseName) {
            this.courseName = courseName;
        }
        public String getCourseName() {
            return courseName;
        }
        public void produceQuestion(Course course, Question question) {
            System.out.println(question.getUserName() + "在" + course.courseName + "提交了一个问题");
            setChanged();
            notifyObservers(question);
        }
    }
    

    问题类

    /**
     * 问题
     */
    public class Question {
        private String userName;
        private String questionContent;
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getQuestionContent() {
            return questionContent;
        }
        public void setQuestionContent(String questionContent) {
            this.questionContent = questionContent;
        }
    }
    

    老师类

    /**
     * 教师是观察者
     */
    public class Teacher implements Observer {
        private String teacherName;
    
        public Teacher(String teacherName) {
            this.teacherName = teacherName;
        }
    
        @Override
        public void update(Observable o, Object arg) {
            Course course = (Course) o;
            Question question = (Question) arg;
            System.out.println(teacherName + "老师的" + course.getCourseName() + "课程接收到一个" + question.getUserName() + "提交的问答:" + question.getQuestionContent());
    
        }
    }
    

    测试

    public class Test {
        public static void main(String[] args) {
            Course course = new Course("数学");
            Teacher teacher1 = new Teacher("Mr.张");
            Teacher teacher2 = new Teacher("Mr.岑");
    
            course.addObserver(teacher1);
            course.addObserver(teacher2);
    
            //业务逻辑代码
            Question question = new Question();
            question.setUserName("小朋友A");
            question.setQuestionContent("这道题目怎么做");
            course.produceQuestion(course,question);
        }
    }
    

    控制台输出

    image-20220115095950521



    3.源码中的应用

    基础知识:

    Listener监听器,eventListener基类是监听器的基类

    spring监听模式需要三个组件:

    1.事件,需要继承ApplicationEvent,即观察者模式中的"主题",可以看做一个普通的bean类,用于保存在事件监听器的业务逻辑中需要的一些字段;

    2.事件监听器,需要实现ApplicationListener,即观察者模式中的"观察者",在主题发生变化时收到通知,并作出相应的更新,加泛型表示只监听某种类型的事件;

    3.事件发布器,需要实现ApplicationEventPublisherAware,获取spring底层组件ApplicationEventPublisher,并调用其方法发布事件,即"通知"观察者。

    其中,事件监听器和事件发布器需要在springIOC容器中注册。


    spring应用:

    在spring的refresh()中有这样一段方法

    // 注册监听器(检查监听器的bean并注册它们)
    registerListeners();
    。。。
    finishRefresh();
    
    //遍历注册监听器,并执行multicastEvent方法,启动任务线程
    protected void registerListeners() {
       // Register statically specified listeners first.
       for (ApplicationListener<?> listener : getApplicationListeners()) {
          getApplicationEventMulticaster().addApplicationListener(listener);
       }
    
       // Do not initialize FactoryBeans here: We need to leave all regular beans
       // uninitialized to let post-processors apply to them!
       String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
       for (String listenerBeanName : listenerBeanNames) {
          getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
       }
    
       // Publish early application events now that we finally have a multicaster...
       Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
       this.earlyApplicationEvents = null;
       if (earlyEventsToProcess != null) {
          for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
             getApplicationEventMulticaster().multicastEvent(earlyEvent);
          }
       }
    }
    
    //publishEvent  发布事件
    protected void finishRefresh() {
       // Clear context-level resource caches (such as ASM metadata from scanning).
       clearResourceCaches();
    
       // Initialize lifecycle processor for this context.
       initLifecycleProcessor();
    
       // Propagate refresh to lifecycle processor first.
       getLifecycleProcessor().onRefresh();
    
       //发布事件
       publishEvent(new ContextRefreshedEvent(this));
    
       // Participate in LiveBeansView MBean, if active.
       LiveBeansView.registerApplicationContext(this);
    }
    
  • 相关阅读:
    linux shell 脚本显示执行的命令与关闭显示执行的命令
    docker-compose 文件参考
    django 关系字段一对多的筛选
    深度学习-tensorflow学习笔记(2)-MNIST手写字体识别
    opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测
    opencv-学习笔记(5)形态学转变
    opencv-学习笔记(4)-模糊
    opencv-学习笔记(3)
    opencv-学习笔记(2)
    opencv-学习笔记(1)常用函数和方法。
  • 原文地址:https://www.cnblogs.com/yslu/p/15806230.html
Copyright © 2011-2022 走看看