Behavioral Patterns  «Prev  Next»

Observer Pattern

The Observer pattern provides a way for a component to flexibly broadcast messages to interested receivers. It defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. The Figure below illustrates the Observer pattern.

Observer Pattern consisting of abstract class Subject and Observer


The following lists the benefits of using the Observer pattern:
  1. Abstract coupling between subject and observer
  2. Support for broadcast communication

When to Use:

You should use the Observer pattern when:
  1. A change to one object requires changing the other object and you do not know how many objects need to change.
  2. An object should be able to notify other objects without making assumptions about the identity of those objects.