23种设计模式 观察者

https://github.com/yzmaodeng/java-keypointknowledge/commit/50b152897d746b26c592c281aba2658b423d3a50


jdk里面的设计

public interface Observer

A class can implement the  Observer interface when it wants to be informed of changes in observable objects
当想充当观察者接收被观察者的变更信息时,可以实现观察者接口。

public class Observable
extends Object

This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed.

An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.


这个类表示模型视图范例中的一个可观察的对象,或“数据”。它可以被子类化以表示应用程序希望观察的对象。
一个可观测对象可以有一个或多个观察者。观察者可以是实现接口观察者的任何对象。在可观察的实例更改之后,调用可观察的NotifyObserver方法的应用程序将通过调用其UPDATE方法来通知其所有的观察者。

The order in which notifications will be delivered is unspecified. The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose.

Note that this notification mechanism has nothing to do with threads and is completely separate from the wait and notify mechanism of class Object.

When an observable object is newly created, its set of observers is empty. Two observers are considered the same if and only if the equals method returns true for them.

未指定通知的传递顺序。可观察类中提供的默认实现将按照它们注册的兴趣顺序通知观察者,但是子类可以更改这个顺序,使用没有保证的顺序,在单独的线程上传递通知,或者可以保证它们的子类按照自己选择的顺序执行。

请注意,此通知机制与线程无关,完全独立于类对象的等待和通知机制。

政府

package com.zl.Designpattern.Observer;


import java.util.Observable;


public class 政府  extends Observable  {

    private String 开发商;  

     

    private String 炒房客;  

     

    private String 购房者;  

     

    public 政府(){};  

     

    public void 政策改变(){  

        setChanged();  

        notifyObservers();  

    }  

     


     

     

    public String get开发商() {

return 开发商;

}


public String get炒房客() {

return 炒房客;

}


public String get购房者() {

return 购房者;

}


// 天气发生变化  

    public void set出政策(String 开发商, String 炒房客, String 购房者){  

        this.开发商 = 开发商;  

        this.炒房客 = 炒房客;  

        this.购房者 = 购房者;  

        政策改变();  

    }  

 


}


市场

package com.zl.Designpattern.Observer;


import java.util.Observable;

import java.util.Observer;


public class 市场反应 implements Observer {

private Observable observable;

private String 开发商;


private String 炒房客;


private String 购房者;


public 市场反应(Observable observable) {

this.observable = observable;

this.observable.addObserver(this);

}


@Override

public void update(Observable observable, Object arg) {

if (observable instanceof 政府) {

政府 zf = (政府) observable;

this.开发商 = zf.get开发商();

this.炒房客 = zf.get炒房客();

this.购房者 = zf.get购房者();

display();

}


}


public void display() {

System.out.println(this.开发商 + "又要发财了");

System.out.println(this.炒房客 + "又可以发财了");

System.out.println(this.购房者 + "现在是不是接盘侠呢");

}


}


test

package com.zl.Designpattern.Observer;


public class test {


public static void main(String[] args) {


政府 zf=new 政府();

市场反应 scfy=new 市场反应(zf);

zf.set出政策("孙宏斌", "肯德基","我");

}


}


结果


孙宏斌又要发财了
肯德基又可以发财了
我现在是不是接盘侠呢




w3c的案例


    abstract class Subject {
        private Vector obs = new Vector();

        public void addObserver(Observer obs){
            this.obs.add(obs);
        }
        public void delObserver(Observer obs){
            this.obs.remove(obs);
        }
        protected void notifyObserver(){
            for(Observer o: obs){
                o.update();
            }
        }
        public abstract void doSomething();
    }

    class ConcreteSubject extends Subject {
        public void doSomething(){
            System.out.println("被观察者事件反生");
            this.notifyObserver();
        }
    }
    interface Observer {
        public void update();
    }
    class ConcreteObserver1 implements Observer {
        public void update() {
            System.out.println("观察者1收到信息,并进行处理。");
        }
    }
    class ConcreteObserver2 implements Observer {
        public void update() {
            System.out.println("观察者2收到信息,并进行处理。");
        }
    }

    public class Client {
        public static void main(String[] args){
            Subject sub = new ConcreteSubject();
            sub.addObserver(new ConcreteObserver1()); //添加观察者1
            sub.addObserver(new ConcreteObserver2()); //添加观察者2
            sub.doSomething();
        }
    }

运行结果

被观察者事件反生

观察者1收到信息,并进行处理。

观察者2收到信息,并进行处理。

通过运行结果可以看到,我们只调用了Subject的方法,但同时两个观察者的相关方法都被同时调用了。仔细看一下代码,其实很简单,无非就是在Subject类中关联一下Observer类,并且在doSomething方法中遍历一下Observer的update方法就行了。

观察者模式的优点

观察者与被观察者之间是属于轻度的关联关系,并且是抽象耦合的,这样,对于两者来说都比较容易进行扩展。

观察者模式是一种常用的触发机制,它形成一条触发链,依次对各个观察者的方法进行处理。但同时,这也算是观察者模式一个缺点,由于是链式触发,当观察者比较多的时候,性能问题是比较令人担忧的。并且,在链式结构中,比较容易出现循环引用的错误,造成系统假死。












猜你喜欢

转载自blog.csdn.net/yz18931904/article/details/80558934