Observer

From HaFrWiki
Revision as of 13:42, 8 April 2015 by Hjmf (talk | contribs)
Jump to: navigation, search

<<Back to Patterns


This article is fully based on Gamma's Design Patterns, Elements of Reusable Object-Oriented Software [1].

Intent

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Motivation

A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. You don't want to achieve consistency by making the classes tightly coupled, because that reduces their reusability.

For example, many graphical user interface toolkits separate the presentational aspects of the user interface from the underlying application data. Classes defining application data and presentations can be reused independently. They can work together, too. Both a spreadsheet object and bar chart object can depict information in the same application data object using different presentations. The spreadsheet and the bar chart don't know about each other, thereby letting you reuse only the one you need. But they behave as though they do. When the user changes the information in the spreadsheet, the bar chart reflects the changes immediately, and vice versa.

Observer

This behavior implies that the spreadsheet and bar chart are dependent on the data object and therefore should be notified of any change in its state. And there's no reason to limit the number of dependent objects to two; there may be any number of different user interfaces to the same data.

The Observer pattern describes how to establish these relationships. The key objects in this pattern are subject and observer. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. In response, each observer will query the subject to synchronize its state with the subject's state.

This kind of interaction is also known as publish-subscribe. The subject is the publisher of notifications. It sends out these notifications without having to know who its observers are. Any number of observers can subscribe to receive notifications.

Applicability

Use the Observer pattern in any of the following situations:

  • When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently.
  • When a change to one object requires changing others, and you don't know how many objects need to be changed.
  • When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.

Structure

Observer-Gamma-002.png

Subject Observer ConcreteSubject ConcreteObserver
  • knows its observers. Any number of Observer objects may observe a subject.
  • provides an interface for attaching and detaching Observer objects.
  • defines an updating interface for objects that should be notified of changes in a subject.
  • stores state of interest to ConcreteObserver objects.
  • sends a notification to its observers when its state changes.
  • maintains a reference to a ConcreteSubject object.
  • stores state that should stay consistent with the subject's.
  • implements the Observer updating interface to keep its state consistent with the subject's.

Collaboration

ToDo

Consequences

Know Usage

The first and perhaps best-known example of the Observer pattern appears in Smalltalk Model/View/Controller (MVC), the user interface framework in the Smalltalk environment.

Related Patterns

  • Mediator, By encapsulating complex update semantics, the ChangeManager acts as mediator between subjects and observers.
  • Singleton, The ChangeManager may use the Singleton pattern to make it unique and globally accessible.

See also

top

Reference

top

  1. Design Pattern, Elements of Reusable Object-Oriented Software, Erich Gamma, Ralph Johnson, Richard Helm, John Vlissides, First Published: October 21, 1994