• Java学习笔记——设计模式之十.观察者模式


     观察者模式(Observer),定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己。

    Subject类:

     1 package cn.happy.design_pattern._10publishsubscribe;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 public class Subject {
     7 
     8     private List<Observer> observers = new ArrayList<>();
     9     
    10     public void attach(Observer observer){
    11         observers.add(observer);
    12     }
    13     
    14     public void detach(Observer observer){
    15         observers.remove(observer);
    16     }
    17     
    18     public void mNotify(){
    19         for (Observer item : observers) {
    20             item.update();
    21         }
    22     }
    23 }

    Observer类:

    1 package cn.happy.design_pattern._10publishsubscribe;
    2 
    3 public abstract class Observer {
    4 
    5     public abstract void update();
    6 }

    ConcreteSubject类:

     1 package cn.happy.design_pattern._10publishsubscribe;
     2 
     3 public class ConcreteSubject extends Subject {
     4 
     5     private String subjectState;
     6 
     7     String getSubjectState() {
     8         return subjectState;
     9     }
    10 
    11     void setSubjectState(String subjectState) {
    12         this.subjectState = subjectState;
    13     }
    14     
    15 }

    ConcreteObserver类:

     1 package cn.happy.design_pattern._10publishsubscribe;
     2 
     3 public class ConcreteObserver extends Observer {
     4     
     5     private String name;
     6     private String observerState;
     7     private ConcreteSubject subject;
     8     
     9     public ConcreteObserver(ConcreteSubject subject, String name) {
    10         this.subject = subject;
    11         this.name = name;
    12     }
    13 
    14     @Override
    15     public void update() {
    16         observerState = subject.getSubjectState();
    17         System.out.println("观察者"+name+"的新状态是"+observerState);
    18     }
    19     //省略getter and setter
    20 }

     测试类:

     1 package cn.happy.design_pattern._10publishsubscribe;
     2 
     3 public class Mmain {
     4 
     5     public static void main(String[] args) {
     6         ConcreteSubject subject = new ConcreteSubject();
     7         subject.attach(new ConcreteObserver(subject, "X"));
     8         subject.attach(new ConcreteObserver(subject, "Y"));
     9         subject.attach(new ConcreteObserver(subject, "Z"));
    10         subject.setSubjectState("ABC");
    11         subject.mNotify();
    12     }
    13 
    14 }
  • 相关阅读:
    ZedBoard学习(6)System Generator实现串口通信(一行HDL代码都不用写)
    ZedBoard学习(1)Ubutun下进行串口通信
    Zedboard学习(7)PS下第一个裸奔程序
    激光雷达(一)数据采集C++
    win7/win8下安装Oracle1出错10g,提示“程序异常终止,发生未知错误”解决方法
    XML文件的加密与解密
    三层中最重要的SqlHelper类
    创建桌面快捷方式的语法
    秋招总结 艾尔夏尔
    thoughtworks二面准备 (三) 艾尔夏尔
  • 原文地址:https://www.cnblogs.com/tomasman/p/7045343.html
Copyright © 2020-2023  润新知