Python – The Observer Design Pattern


Understanding observer design pattern

In the software world, multiple services interact with each other to perform huge operation.Services can perform multiple operations, but the operations are dependent upon the objects of the services that it interacts with.

Let us assume that you follow every blogs posted in debuggers hub. To check out new blogs posted or edited you need to subscribe debuggers hub.There would be multiple subscribers that are also registered with this blog.So whenever there is a new blog posted to debuggers hub , you will be notified, or if there is a change on the published blog, you are also made aware of the edits. The way that debuggers hub notify you could be through email or any other notification mechanism defined by the observer.

The main intentions of observer pattern are listed below:

  • The core concept of observer pattern is to maintain one to many dependancy between objects. It makes sure that any change in one object will be notified to other dependent objects automatically

  •  Encapsulates the core component of the subject.

There are three main participants in observer design pattern:

  1. Subject

  2. Observer

  3. ConcreteObserver


Real World implementaion of observer design pattern

We will take an example of this website (debuggershub).Let us implement observer design pattern in python. Subject behaviour is represented by BlogPublisher class, BlogPublisher is an interface for the subscribers to view. attach() method is user by the observer to register into the BlogPublisher and detach()method for deregistering the observer. subscriber()method returns list of registered subscribers. notifySubscriber() method iterates over the registered subscribers. addBlog() method is used by publisher to create Blog. getBlog()method is used to return latest blog which is notified to the Observer.
Now let us have a look at the Observer interface. Here subscriber represents the Observer. Subscriber is a abstract base class which represents other ConcreteObservers. Subscribe class has update() method which is implemented by ConcreteObserversupdate() method is implemented by ConcreteObservers so that they get notified by BlogPublishers.
Most probably there are two main features of the blog sites to notify Subscribers about the updates which are through SMS and Email. In our case we have SMSSubscribers and EmailSubscribers.The init() method of these ConcreteObservers register them with BlogPublisher with attach() method and update()method is used by BlogPublisher to notify the observers.
The client creates an object for BlogPublisher used by ConcreteObservers. Subscribers are initialized with publisher object.init() method usesattach() method to register. Then we print the list of regsiterd subscribers from the Subject.notifySubscribers()calls update()method and is used to notify subscribers about the added blog. BlogPublisher also as detach() method to deregister subscribers.

View the code as well as output in the github source:

We will go through  proxy design pattern in the next topic of design patterns in python. 

You may also like...

1 Response

  1. July 29, 2020

    […] will go through observer design pattern pattern in the next topic of design patterns in […]

Leave a Reply

Your email address will not be published. Required fields are marked *