Observer Pattern

-> The observer pattern is a design pattern that defines a link between objects so that one object’s stat changes, all dependent objects are updated automatically. This pattern allows communication between objects in a loosely coupled manner.

-> The Observer design pattern defines a one-to-many dependency between a subject object and any number of observer objects, so that when the subject changes state, all its observer subjects are notified and updated automatically.
-> Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Real worlds Example

-> A wordpress site or blogger site, where users post their articles on blog. An interest reader may not be able to visit a blog regularly, to check for a new post. So what the site does is, allow a user to subscribe to the blogger’s posts. So whenever         a blogger posts any article, the people who have subscribed to the blogger will get to know about the new post by email notifications.

-> Knows its observers. Any number of Observer objects may observe a Subject.
-> Provides an interface for attaching and detaching Observer objects.

-> Defines an updating interface for objects that should be notified of changes in a Subject.

Concrete Subject
-> Stores the state of interest to ConcreteObserver objects.
-> Sends a notification to its observers when its state changes.

Concrete Observer
-> Maintains a reference to a ConcreteSubject object.
-> Stores state that should stay consistent with the subject’s.

Leave a Reply

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