• JAVA设计模式---观察者模式


    观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有的依赖者都会收到通知并自动更新,提供了一种对象设计,让主题和观察者之间松耦合,实现代码如下:

    1、主题部分:

      定义主题接口:

    public interface Subject {
        /*增加观察者*/
        public void addObserver(Observer Observer);
    
        /*删除观察者*/
        public void delObserver(Observer Observer);
    
        /*通知所的观察者*/
        public void notifyObserver();
    
        /*自身的操作*/
        public void operation();
    }
    

      主题实现:

    public class SubSubject implements Subject{
        private List<Observer> observers;
        private float temp;
        private float humidity;
        private float pressure;
    
        public SubSubject() {
            observers = new ArrayList<Observer>();
        }
    
        @Override
        public void addObserver(Observer Observer) {
            observers.add(Observer);
        }
    
        @Override
        public void delObserver(Observer Observer) {
            int i = observers.indexOf(observers);
            if(i>=0){
                observers.remove(i);
            }
        }
    
        @Override
        public void notifyObserver() {
            for(int i=0;i<observers.size();i++){
                Observer observer = observers.get(i);
                observer.update(temp,humidity,pressure);
            }
        }
    
        @Override
        public void operation() {
            notifyObserver();
        }
    
        public void setParameters(float temp,float humidity,float pressure){
            this.temp = temp;
            this.humidity = humidity;
            this.pressure = pressure;
            operation();
        }
    }
    

      2、观察者部分:

      定义观察者接口:

    public interface Observer {
        public void update(float temp,float humidity,float pressure);
    }
    

      定义展示元素接口:

    public interface DisplayElement {
        public void display();
    }
    

      观察者1实现:

    public class Observer1 implements Observer,DisplayElement {
        private float temp;
        private float humidity;
        private float pressure;
        private SubSubject mySubject1;
    
        /*保存对Subject的引用,以后如果想取消注册,可以用到*/
        public Observer1(SubSubject mySubject1) {
            this.mySubject1 = mySubject1;
            mySubject1.addObserver(this);
        }
    
        @Override
        public void update(float temp, float humidity, float pressure) {
            this.temp = temp;
            this.humidity = humidity;
            this.pressure = pressure;
            display();
        }
    
        @Override
        public void display() {
            System.out.println("Observer1: temp="+temp+",humidity="+humidity+",pressure="+pressure);
        }
    }
    

      观察者二实现:

    public class Observer2 implements Observer,DisplayElement {
        private float temp;
        private float humidity;
        private float pressure;
        private SubSubject mySubject1;
    
        /*保存对Subject的引用,以后如果想取消注册,可以用到*/
        public Observer2(SubSubject mySubject1) {
            this.mySubject1 = mySubject1;
            mySubject1.addObserver(this);
        }
    
        @Override
        public void update(float temp, float humidity, float pressure) {
            this.temp = temp;
            this.humidity = humidity;
            this.pressure = pressure;
            display();
        }
    
        @Override
        public void display() {
            System.out.println("Observer2: temp="+temp+",humidity="+humidity+",pressure="+pressure);
        }
    }
    

      3、测试:

      测试类:

    public class ObserverTest {
        public static void main(String[] args) {
            SubSubject sub = new SubSubject();
            Observer1 observer1 = new Observer1(sub);
            Observer2 observer2 = new Observer2(sub);
    
            sub.setParameters(80,65,30.1f);
            sub.setParameters(70,55,20.1f);
        }
    }
    

      测试结果:

    Observer1: temp=80.0,humidity=65.0,pressure=30.1
    Observer2: temp=80.0,humidity=65.0,pressure=30.1
    Observer1: temp=70.0,humidity=55.0,pressure=20.1
    Observer2: temp=70.0,humidity=55.0,pressure=20.1

  • 相关阅读:
    MongoDB 3.0安全权限访问控制(Windows版)
    MVC创建通用DropdownList
    当文字超出宽度,自动截取文字并加省略号
    JQuery Datatables(二)
    JQuery Datatables(一)
    PHP signal 信号
    phpunit 入门
    Wget 命令详解
    ubuntn下 apt的用法和yum的比较(转)
    navicat for mysql 安装
  • 原文地址:https://www.cnblogs.com/hunterCecil/p/5673411.html
Copyright © 2020-2023  润新知