Observer Design Pattern is a design pattern in Python that facilitates a one-to-many relationship. Say, for example, you and several other readers subscribed to a newsletter. When there is a new newsletter available, you will receive it along with other subscribers. Suppose, if you don’t want to receive the newsletter, you can cancel your subscription and you will not receive the new editions.
An observer design pattern is not limited to the newsletter subscription. It can be any information such as a concurrent thread signal, a signal from an operating system, and so on. An observer design pattern is a form of a publishing-subscriber pattern. It facilitates managing subscriptions and broadcasting information to subscribers.
Benefits of Observer Design Pattern
Observer Design Pattern has a static object called Subject and a variable object called Observers. There can be zero or N number of observers, which can change based on subscription. Here, the Subject keeps the Observers, and if any of the object state changes, the Subject notifies other Observers. For example, consider the case of a LinkedIn post. When you post a new LinkedIn post (state change), the timeline of your followers is updated with your new post.
Let’s look into its benefits.
- Facilitates a loose coupling between Subject and Observers
- Observers can be updated at runtime
- A subject can keep zero or N number of observers
- The aptitude of broadcasting messages between Subject and Observers.
Newsletter Subscription Implementation
Let’s design a simple newsletter subscription model to understand the observer design pattern. As we discussed, an observer design pattern has two main objects – Subject and Observer. The subject can add, remove, and notify the observers through register_observer, unregister_observer, and notify_observer. Whereas, the observer is an interface that has an abstract method – notify.
Here, two concrete observers – CompanyNewsletterObserver and ConsumerNewsletterObserver – are derived from the Observer interface. These concrete methods implement the abstract method notify, and the Subject will call the notify method in its notify_observer method. So to send information to the subscribers, we simply need to call the notify_observer method in the Subject. Let’s look into the implementation.
company_newsletter_observer : 2020-10-15 20:40:04.335355
company_newsletter_observer : 2020-10-15 20:40:06.336913
consumer_newsletter_observer : 2020-10-15 20:40:06.336913
consumer_newsletter_observer : 2020-10-15 20:40:08.339662
You can refer to the above output to understand the behavior of code while registering and un-registering observers.
An observer design pattern best suits the need to achieve a one-to-many relationship. You can broadcast the same information to many listeners. And, the listeners can be added or removed at runtime. Here, the subject only has information about the observer’s interface, which helps to maintain loose coupling between the subject and the observers.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course