Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "OTExample Observer/Main"

(New page: ===Main program driving the Observer example=== <source lang="otj"> package flower_example; /** * @author Bruce Eckel (original Java example) * @author Miguel P. Monteiro (adaptation to...)
 
 
Line 1: Line 1:
 
===Main program driving the Observer example===
 
===Main program driving the Observer example===
  
<source lang="otj">
+
<source lang="otj" line="1">
 
package flower_example;
 
package flower_example;
 
/**
 
/**
Line 59: Line 59:
 
}
 
}
 
</source>
 
</source>
 +
 +
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.

Latest revision as of 05:24, 24 February 2010

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.

Back to the top