Skip to main content
Jump to: navigation, search

OTExample Observer/ObservingClose

< OTExample Observer
Revision as of 06:42, 23 February 2010 by Stephan.cs.tu-berlin.de (Talk | contribs) (New page: '''Application of the Observer pattern''' This team is a slight variation of ObservingOpen, also binding the [[OTExample Observer/ObserverPattern|Obse...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Application of the Observer pattern

This team is a slight variation of ObservingOpen, also binding the ObserverPattern like this:

A few comments are given inline, for more explanation see below.

  1. package flower_example;
  2.  
  3. import protocol.ObserverPattern;
  4. /**
  5.  * @author Bruce Eckel (original Java example)
  6.  * @author Miguel P. Monteiro (adaptation to OT/J)
  7.  */
  8. public team class ObservingClose extends ObserverPattern {
  9.  
  10.     protected class Subject playedBy Flower
  11.     {
  12.         // This method binding uses a private field of its base class: isOpen
  13.         @SuppressWarnings("decapsulation")
  14.         changeOp <- before close
  15.             base when (base.isOpen);
  16.     }
  17.  
  18.     public class BeeObserver extends Observer playedBy Bee {
  19.         update -> goToBed;
  20.     }
  21.  
  22.     public class HummingbirdObserver extends Observer playedBy Hummingbird {
  23.         update -> bedTime;
  24.     }
  25.  
  26.     // This and the following signature applies OTJLD §2.3.2(e):
  27.     public <AnyBase base Observer>
  28.     void mapSubject2Observer(Flower as Subject subject, AnyBase as Observer observer) {
  29.         subject.addObserver(observer);
  30.     }
  31.  
  32.     public <AnyBase base Observer>
  33.     void removeObserverFromSubject(Flower as Subject subject, AnyBase as Observer observer) {
  34.         subject.removeObserver(observer);
  35.     }
  36.  
  37.     public void removeAllObserversFromSubject(Flower as Subject subject) {
  38.         subject.removeAllObservers();
  39.     }
  40. }

Differences to ObservingOpen

soon

Back to the top