• Mainly used to implement distributed event handling systems.

  • A way of notifying change to a number of classes

Background:

In Object Oriented Programming world each and every object is responsible for its own functionalities and sometimes one object needs to depend upon anther object. If we implement logic (tight coupling) in one object (observer) to monitor or track of another object (source) functionality, it will make tight coupling dependency between these 2 objects. If any changes need to be done in source object requires making changes in target object (observer).

Solution:

We should allow loose coupling (no one object depends upon other objects functionality) between the objects. Every object should take care of its functionality and if we need any information from another object it should notify automatically.

Types of a pattern:

  • Observer, Subject Model:

    • A design pattern in which an object called the subject, maintains a list of its dependents called observers and notifies them automatically of any state changes, usually by calling one of their methods.

  • Publisher, Subscriber Model:

    A design pattern in which an object called the publisher, maintains a list of its dependents called subscribers and notifies them automatically of any state changes, usually by calling one of their methods.

 

Reference Example