UniKL Logo

Lehrgebiet Informationssysteme

FB Informatik

FB Informatik
 
LG IS
AG DBIS
AG HIS
Jobs / Tasks
Courses
Publications
Contact
Misc
Impressum
(C) AG DBIS
 

Introduction

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

Participants:

Subject

  • knows its observers. Any number of Observer objects may observe a subject.
  • provides an interface for attaching and detaching Observer objects.
  • this participant can not be read-only.

Observer

  • defines an updating interface for objects that should be notified of changes in a subject.
  • this participant can not be read-only.

Concrete Subject

  • stores state of interest to ConcreteObserver objects.
  • sends a notification to its observers when its state changes.
  • this participant can not be read-only.
  • this participant can not be an interface.

Concrete Observer

  • 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.
  • this participant can not be read-only.

The "Copy documentation" option controls whether to copy JavaDoc comments from methods in interfaces participating in the pattern to the stubs of these methods made by the pattern in classes implementing such interfaces.

The "Create pattern links" option controls whether to generate additional links which can be used by this pattern later to determine classes and interfaces participating in the pattern. That means that if you selected this option and used the pattern to create a set of classes and interfaces, the pattern invoked for some participant later (via the popup menu item "Choose Pattern...") will automatically find (if it is possible) all other participants and fill in participant fields with their names.

Also, if you applied the pattern with this option on and invoked the pattern via the popup menu item "Choose Pattern..." for some participant later, the pattern will have additional field called "Use selected class as", containing possible roles for the selected element.

This option is very useful when you are planning to change something in the classes/interfaces participating in the pattern. For example, if this option is on and after creation of the classes and interfaces you added several methods to certain interface-participant (and this change must be reflected somehow in other participants), all you have to do is to select this changed interface, invoke the "Choose Pattern..." dialog for this element and select the original pattern. After that the pattern will determine other participants and you will only have to press "Finish". The pattern will modify all other classes and interfaces accordingly to your changes.

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.