System Design ( Day - 48 )

Observer Design Pattern
Definition
Defines a One-to-many Relationship between objects so that when one object changes state, all of its dependents are notified and updated automatically.
The real world example would be like this when you subscribed to any Youtube channel you are the observer and the Youtube channel is the Observable if the state of the Youtube channel is changed like if any video is uploaded then that is observer you you or subscribers.
Pooling Technique.
In this method we’ll do like the subscriber has to ask the Youtube channel that is there any new video uploaded? in some time interval, this is bad way of doing this thing because every time the subscriber has to ask and the request and response would be high and the performance will be low.
Pushing Technique.
In this method the Youtube channel knows the Subscribers whenever there is a new video uploaded the channel will push the update to it’s subscribers, this is a better approach because we are not pooling the update from the subscriber, youtube uses this method to push the notifications to the subscribers.
Youtube Example
In this youtube example we have 2 abstract classes which are channel and subscriber, in the channel we have subscribe , unsubscribe and notify method are there in subscriber we have notify method is there, these has to be defined in the concrete classes, when a new video is uploaded in the channel that is notified to the subscriber
This Observer design pattern will break Single responsibility principle, because as you can see in the image its doing subscribe, unsubscribe, uploadNewVideo and also its notifying the observer.
Real world Example
1. Notification System
2. Event listeniers in Frontend
Subscribe to my newsletter
Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
