zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    仿windows选项卡效果拾零(收藏)
    把一个字符串分开存入一个临时表中
    DOM的基本方法
    如何判断iframe加载完毕(原创)
    javascript中showModalDialog和showModelessDialog的使用(转)
    一个sql子查询作为过滤条件的例子(原创)
    关闭窗口,弹出对话框
    设置C#程序在Windows 7 Vista下以管理员权限运行(转)
    SQL SERVER 6 视图与索引
    SQL SERVER 各类触发器的完整语法及参数说明(拓展)
  • 原文地址:https://www.cnblogs.com/hunterCecil/p/5673411.html
Copyright © 2011-2022 走看看