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.
- Observer method - Python Design Patterns
- Youtube Data API Subscription | Set-1
- Youtube Data API Subscription | Set-2
- Youtube Data API Subscription | Set-3
- Implementing Weather Forecast using Facade Design Pattern in Python
- Implementing News Parser using Template Method Design Pattern in Python
- Implementing Web Crawler using Abstract Factory Design Pattern in Python
- Accessing Web Resources using Factory Method Design Pattern in Python
- Mediator Method - Python Design Pattern
- Proxy Design Pattern for Object Communication in Python
- Implementing web scraping using lxml in Python
- Python | Implementing 3D Vectors using dunder methods
- Python | Implementing Dynamic programming using Dictionary
- Implementing Rich getting Richer phenomenon using Barabasi Albert Model in Python
- Post/Redirect/Get (PRG) Design Pattern
- Implementing DBSCAN algorithm using Sklearn
- Implementing Agglomerative Clustering using Sklearn
- ML | Implementing L1 and L2 regularization using Sklearn
- Implementing Deep Q-Learning using Tensorflow
- ML | OPTICS Clustering Implementing using Sklearn
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.