Skip to main content
Jump to: navigation, search

OTExample Observer/Main

< OTExample Observer
Revision as of 06:47, 23 February 2010 by Stephan.cs.tu-berlin.de (Talk | contribs) (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...)

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

Main program driving the Observer example

package flower_example;
/**
 * @author Bruce Eckel (original Java example)
 * @author Miguel P. Monteiro (adaptation to OT/J)
 *
 * Driver for this example of Observer
 */
public class Main {
    public static void main(String[] args) {
        ObservingOpen openingObs = new ObservingOpen();
        ObservingClose closingObs = new ObservingClose();
        openingObs.activate();
        closingObs.activate();
 
        Flower flower = new Flower();
        Bee beeA = new Bee("A");
        Bee beeB = new Bee("B");
        Hummingbird birdA = new Hummingbird("A");
        Hummingbird birdB = new Hummingbird("B");
 
        openingObs.mapSubject2Observer(flower, birdA);
        openingObs.mapSubject2Observer(flower, birdB);
        openingObs.mapSubject2Observer(flower, beeA);
        openingObs.mapSubject2Observer(flower, beeB);
 
        closingObs.mapSubject2Observer(flower, birdA);
        closingObs.mapSubject2Observer(flower, birdB);
        closingObs.mapSubject2Observer(flower, beeA);
        closingObs.mapSubject2Observer(flower, beeB);
 
        System.out.println("Day 1: all observers follow the flower:");
        flower.open();
        flower.close();
 
        System.out.println("Day 2: Hummingbird B decides to sleep in.");
        openingObs.removeObserverFromSubject(flower, birdB);
        flower.open();
        System.out.println("   The flower is already open, no change:");
        flower.open();
 
        System.out.println("Bee A doesn't want to go to bed.");
        closingObs.removeObserverFromSubject(flower, beeA);
        flower.close();
        System.out.println("   The flower is already closed, no change:");
        flower.close();
 
        System.out.println("Day 3: All observers lose interest in the flower's opening.");
        openingObs.removeAllObserversFromSubject(flower);
 
        flower.open();
        flower.close();
        openingObs.deactivate();
        closingObs.deactivate();
        System.out.println("\nterminated.");
    }
}

Back to the top