2018.5.1 Observer Pattern of Design Patterns

https://www.cnblogs.com/tongkey/p/7170826.html

https://www.w3cschool.cn/javadesignpattern/e1w91ihr.html

The Observer Pattern of Design Patterns (Observer Pattern)

1. Definition, Concept

    有时又称为模型---视图模式、源收听者模式或从属者模式是软件设计模式的一种,再此 模式中,一个目标物件管理所有想已于他的观察者;
    观察者模式(Observer)完美的将观察者和被观察的对象分离开。   
    定义对象之间的一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新。

2. Implementation

    观察者模式有很多实现方式,从根本上说,该模式必须包含两个角色:观察者和观察对象。
    实现观察者模式的时候要注意,观察者个被观察对象之间的互动关系不能体现成类之间的直接调用,否则就将
使观察者和被观察对象之间的紧密的偶合起来,从根本上违反面向对象的设计的苑子文。无论是
观察者模式“”观察“观察对象,还是被观察者将自己的改变“”通知”观察者,都不应该直接调用

3. Observer pattern structure

(1) Observed:

    从类图中可以看到,类中有一个用来存放观察者对象的Vector容器(之所以使用Vector而不使用List,是因为多线程操作时,Vector在是安全的,而List则是不安全的),这个Vector容器是被观察者类的核心,另外还有三个方法:attach方法是向这个容器中添加观察者对象;detach方法是从容器中移除观察者对象;notify方法是依次调用观察者对象的对应方法。这个角色可以是接口,也可以是抽象类或者具体的类,因为很多情况下会与其他的模式混用,所以使用抽象类的情况比较多。
观察者:观察者角色一般是一个接口,它只有一个update方法,在被观察者状态发生变化时,这个方法就会被触发调用。

(2) The specific observed person:

    使用这个角色是为了便于扩展,可以在此角色中定义具体的业务逻辑。

(3) Specific observers:

    观察者接口的具体实现,在这个角色中,将定义被观察者对象状态发生变化时所要处理的逻辑。

4. Code implementation

(2) Common code of the observed

public abstract class Subject {
     //定义一个观察者数组
     private Vector<Observer> obsVector = new Vector<Observer>();
     //增加一个观察者
     public void addObserver(Observer o){
             this.obsVector.add(o);
     }
     //删除一个观察者
     public void delObserver(Observer o){
             this.obsVector.remove(o);
     }
     //通知所有观察者
     public void notifyObservers(){
             for(Observer o:this.obsVector){
                     o.update();
        }
     }
}

###5、使用场景

(1) Associated behavior scenarios. It's important to note that association behaviors are splittable, not "composed" relationships.
(2) Event multi-level departure scene.
(3) Cross-system message exchange scenarios, such as the processing mechanism of message queues.
(4) Broadcast chain problem
In an observer mode, there is at most one object, that is, the observer and the observer, that is to say, the message is forwarded at most once (delivered twice)
(5) Asynchronous processing problem
There are many observers, and processing The time is relatively long, and asynchronous processing is used to consider thread safety and queue issues.


###6、优缺点
观察者与被观察者之间是属于轻度的关联关系,并且是抽象耦合的,这样,对于两者来说都比较容易进行扩展
观察者模式是一种常用的触发机制,它形成一条触发链,依次对各个观察者的方法进行处理,但同时,这也算

It is a disadvantage of the observer. Because of the chain trigger, when there are many observers, the performance problem is more worrying, and in the chain structure, it
is more prone to circular reference errors, causing the system to freeze.
````

7. Summary

In the Java language, there is an interface Observer, and its implementation class Observable, which often implements the observer role; if you want to design an event-triggered processing mechanism, using the observer mode is a good choice. Events in AWT Handling DEM (Delegation Event Model) is implemented using the Observer pattern.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339152&siteId=291194637