observe the Observer Design Pattern

赵佶铖
ELP-2018
Published in
4 min readJan 30, 2019

When we are programming, whatever the language used, we meet a lot of similar problems.The design patterns adapt to this situation for solving easier. Erich Gamma and his 3 coworkers introduced this general and reusable solution from an architectural concept. Then, these 4 programmer, also called GoF, have summarized the 23 kinds of design pattern in their book and I will present you the observer design pattern.

How can a class know the change of another one which has no inheritance relation with it? Just like the RSS ( Really Simple Syndication: it allows a user to keep track of many different websites in a single news aggregator.) subscription, you don’t monitor all the time but it will inform you when it refreshes. For solving this kind of design problems, GoF created the Observer design pattern.

Observer design pattern, also known as publish-subscribe patterns, is one of the behavioral pattern which pay attention to the relation between the objects. It shows an one-to-many dependency between the objects. Among them, an object called the subject maintain the connexion with the other objects called observers. When the subject changes, it will inform the observers and they will update according to the information. Of cause, the observers should be registered in the Subject, otherwise no one will inform them. Just like an exemple in our life, a newspaper never informs a person who doesn’t subscribe its service, but if he is its client and he will get information every time immediately.

The observer pattern defines four roles:

Subject:

It’s an abstract class and define the method of adding,reducing and informing the objects.

ConcreteSubject:

It’s a concrete class and inherit the Subject and has a list of observers. When it makes a change, it will inform all the observers which is stored in itself.

Observer:

It’s an abstract class and define an update method.

ConcreteObserver:

It’s a concrete class and update itself when receiving the information.

Here is an exemple of observer pattern in java:

When the Observer receive the information, it will start the updating funcion.

There are 2 reaction funcions for the information. They will all be executed after the reception.

Subject is used to manage the observers.

Concrete Subject contains the observer list and the functions treating them

The result of execution is:

update self!

observer1 has received!

(This is the result of the update funcion. It means that the observer has received the information et update itself.)

observer2 has received!

After the change of Subject, observers update itself according to the observer pattern.

But the observer pattern may cause some problems in the special case. The observers are independent. When one of the update function has an error, the others won’t know it and they will continue to run normally. So it’s better to add a try-catch block in the update function.

After reading this article, you should know the Observer design pattern and how to solve the similar problems.

Source:

(This is a Chinese website that I consulted for understand this pattern)

--

--