The observer pattern
This is a very common design pattern and can be seen being implemented in many areas of software applications, such as (Model View Controller (MVC), event handling, and almost all GUI toolkits. If we take an example of MVC, then "View" informs all its dependents such as the controller whenever there is any change in the UI.
This design pattern is also referred to as the publisher-subscriber pattern frequently. It works on the principle of don't call us - we'll call you. The publisher, in this case, notifies all its subscribers whenever required.
Note
The observer pattern is a behavioral design pattern in which an object (a subject or publisher) maintains a list of all its dependents (an observer or subscriber) and notifies them whenever there is any change in its state.
Following are the thumb rules to implement the observer pattern:
- The
Subject
class maintains a list of all its dependent observers with additional methods, such assubscribe
,unsubscribe
,notify
, and so on...