Behavioural Design Pattern: Observer Design pattern

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:01:00

Define Observer Design Pattern

This pattern is used when there is a one-to-many relationship between things, such as when one object is modified, all dependent objects are automatically alerted. This design enables a single object, known as the subject, to publish changes to its state, and other observer objects that rely on the subject are immediately notified of any changes to its state.

Observer Pattern with UML diagram

The classes, interfaces, and objects in the above UML class diagram are defined as follows.

  • Subject: This class includes a private collection of observers who have subscribed to a subject for notice via the Notify action.
  • ConcreteSubject: This class has its state. When its state changes, the object notifies all of its observers using the base class's Notify action.
  • Observer: This interface defines the operation Update, which will be invoked when the subject's state changes.
  • ConcreteObserver: This class implements the Observer interface and analyses the subject to see what information has changed.

When should you use the Observer Design Pattern?

The Observer design pattern is appropriate for the following cases:

  • Changes in the state of an item must be communicated to a subset of dependent objects, not all of them.
  • Notification capabilities are required.
  • The object providing the notice does not need to be aware of the receivers' objects.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this