Jump to: navigation, search

OTExample Observer/Main

Main program driving the Observer example

  1. package flower_example;
  2. /**
  3.  * @author Bruce Eckel (original Java example)
  4.  * @author Miguel P. Monteiro (adaptation to OT/J)
  5.  *
  6.  * Driver for this example of Observer
  7.  */
  8. public class Main {
  9.     public static void main(String[] args) {
  10.         ObservingOpen openingObs = new ObservingOpen();
  11.         ObservingClose closingObs = new ObservingClose();
  12.         openingObs.activate();
  13.         closingObs.activate();
  14.  
  15.         Flower flower = new Flower();
  16.         Bee beeA = new Bee("A");
  17.         Bee beeB = new Bee("B");
  18.         Hummingbird birdA = new Hummingbird("A");
  19.         Hummingbird birdB = new Hummingbird("B");
  20.  
  21.         openingObs.mapSubject2Observer(flower, birdA);
  22.         openingObs.mapSubject2Observer(flower, birdB);
  23.         openingObs.mapSubject2Observer(flower, beeA);
  24.         openingObs.mapSubject2Observer(flower, beeB);
  25.  
  26.         closingObs.mapSubject2Observer(flower, birdA);
  27.         closingObs.mapSubject2Observer(flower, birdB);
  28.         closingObs.mapSubject2Observer(flower, beeA);
  29.         closingObs.mapSubject2Observer(flower, beeB);
  30.  
  31.         System.out.println("Day 1: all observers follow the flower:");
  32.         flower.open();
  33.         flower.close();
  34.  
  35.         System.out.println("Day 2: Hummingbird B decides to sleep in.");
  36.         openingObs.removeObserverFromSubject(flower, birdB);
  37.         flower.open();
  38.         System.out.println("   The flower is already open, no change:");
  39.         flower.open();
  40.  
  41.         System.out.println("Bee A doesn't want to go to bed.");
  42.         closingObs.removeObserverFromSubject(flower, beeA);
  43.         flower.close();
  44.         System.out.println("   The flower is already closed, no change:");
  45.         flower.close();
  46.  
  47.         System.out.println("Day 3: All observers lose interest in the flower's opening.");
  48.         openingObs.removeAllObserversFromSubject(flower);
  49.  
  50.         flower.open();
  51.         flower.close();
  52.         openingObs.deactivate();
  53.         closingObs.deactivate();
  54.         System.out.println("\nterminated.");
  55.     }
  56. }

This main program uses no special OT/J syntax, yet it applies some concepts specific to Object Teams:

Team activation

Lines 10-13 instantiate and activate the two pattern applications (OTJLD §5). Similarly lines 52 and 53 clean up by deactivating both team instances.

Team methods

Lines 21ff, 36, 42 and 48 invoke team methods which have declared lifting OTJLD §2.3.2. Note that to clients these methods look like regular Java methods, the as part of the signature is not part of the public API.

If you run this program you will notice how the reactions of the observers varies as they are register with and unregister from the pattern. Also a double open has no further effects.