Skip to main content
Jump to: navigation, search

OTExample Observer/ObservingOpen

< OTExample Observer
Revision as of 06:38, 23 February 2010 by Stephan.cs.tu-berlin.de (Talk | contribs) (New page: '''Application of the Observer pattern''' This team binds the ObserverPattern: * The Subject role is played by Flower ...)

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

Application of the Observer pattern

This team binds the ObserverPattern:

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 ObservingOpen extends ObserverPattern {
  9.  
  10.     protected class Subject playedBy Flower
  11.     {
  12.         // This method  implicitly uses a private field of its base class: isOpen
  13.         @Override
  14.         @SuppressWarnings({ "inferredcallout", "decapsulation" })
  15.         public void changeOp() {
  16.             // manual implementation, see ObservingClose.Subject for an abbreviation.
  17.             if(!isOpen)
  18.                 tsuper.changeOp();
  19.         }
  20.         changeOp <- before open;
  21.     }
  22.  
  23.     public class BeeObserver extends Observer playedBy Bee {
  24.         update -> haveBreakfast;
  25.     }
  26.  
  27.     public class HummingbirdObserver extends Observer playedBy Hummingbird {
  28.         update -> breakfastTime;
  29.     }
  30.  
  31.     // This and the following signature applies OTJLD §2.3.2(e):
  32.     public <AnyBase base Observer>
  33.     void mapSubject2Observer(Flower as Subject subject, AnyBase as Observer observer) {
  34.         subject.addObserver(observer);
  35.     }
  36.  
  37.     public <AnyBase base Observer>
  38.     void removeObserverFromSubject(Flower as Subject subject, AnyBase as Observer observer) {
  39.         subject.removeObserver(observer);
  40.     }
  41.  
  42.     public void removeAllObserversFromSubject(Flower as Subject subject) {
  43.         subject.removeAllObservers();
  44.     }
  45. }

Explanation: soon

Back to the top