81
Observer Pattern
An Observer Pattern says that “just define a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically”.
The observer pattern is also known as Dependents or Publish-Subscribe.
Benefits:
- It describes the coupling between the objects and the observer.
- It provides the support for broadcast-type communication.
Usage:
- When the change of a state in one object must be reflected in another object without keeping the objects tight coupled.
- When the framework we writes and needs to be enhanced in future with new observers with minimal chamges.
UML for Observer Pattern:
Implementation of Observer Pattern
Step 1:
Create a ResponseHandler1 class the will implement the java.util.Observer interface.
Step 2:
Create a ResponseHandler2 class the will implement the java.util.Observer interface.
Step 3:
Create an EventSource class that will extend the java.util.Observable class .
Output:
Next Topicstate Design Pattern