Object Oriented Software Engineering   View all facts   Glossary   Help
subject > pattern > design pattern > observer
Next design patternplayer-role    Updesign pattern    Previous design patternimmutable   

observer
subjectfact 
observercan be used to separate the model^2 from the controller in the Model-View-Controller architecture2001-08-30 14:56:49.0
has definition A pattern found in class diagrams in which instances of one class are informed of changes to instances of a second class2001-08-30 14:56:49.0
has antipatterns
  • connecting an observer directly to an observable so that they both have references to each other
  • making the observers subclasses of the observable
2001-08-30 14:56:49.0
has context
  • When you create a two-way association between two classes, the code for the classes becomes inseparable
  • When you compile one, the other one has to be available since the first one explicitly refers to it
  • This means if you want to reuse one of the classes, you also have to reuse the other; similarly, if you change one, you probably have to change the other
2001-08-30 14:56:49.0
has forces
  • You want to maximize the flexibility of the system to the greatest extent possible.
2001-08-30 14:56:49.0
has problem How do you reduce the interconnection between classes, especially between classes that belong to different modules or subsystems?2001-08-30 14:56:49.0
has references one of the Gang of Four patterns.2001-08-30 14:56:49.0
has solution
  1. Create an abstract class «Observable» that maintains a collection of «Observer» instances.
  2. The «Observable» class is very simple; it merely has a mechanism to add and remove observers, as well as a method notifyObservers that sends an update message to each «Observer».
  3. Any application classes can declare itself to be a subclass of «Observable».
  4. «Observer» is an interface, defining only an abstract update method.
  5. Any class can thus be made to observe an Observable by declaring that it implements the interface, and asking to be a member of the observer list of the «Observable».
  6. The «Observer» can then expect calls to its update method whenever the «Observable» changes.
  7. Using this pattern, the «Observable» neither has to know the nature of the classes that will be interested in receiving the update messages, nor what they will do with this information.
2001-08-30 14:56:49.0
is a subtopic of 6.6 - The Observer Pattern2001-08-30 14:56:50.0
is an instance of design pattern2001-08-30 14:56:50.0
design patternhas name2001-08-30 14:55:16.0
has related patterns zero or more related design patterns2001-08-30 14:55:16.0
should be illustrated using a simple diagram2001-08-30 14:55:16.0
should be written using a narrative writing style2001-08-30 14:55:16.0
patternshould be as general as possible2001-08-30 14:56:56.0
should be described in an easy-to-understand form so that people can determine when and how to use it2001-08-30 14:56:56.0
should contain a solution that has been proven to effectively solve the problem in the indicated context2001-08-30 14:56:56.0

Next design patternplayer-role    Updesign pattern    Previous design patternimmutable