Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
OTExample Observer/ObservingClose
< OTExample Observer
Revision as of 07:42, 23 February 2010 by Unnamed Poltroon (Talk) (New page: '''Application of the Observer pattern''' This team is a slight variation of ObservingOpen, also binding the [[OTExample Observer/ObserverPattern|Obse...)
Application of the Observer pattern
This team is a slight variation of ObservingOpen, also binding the ObserverPattern like this:
- The Subject role is played by Flower
- The Observer role is played by Bee and Hummingbird
A few comments are given inline, for more explanation see below.
package flower_example;
import protocol.ObserverPattern;
/**
* @author Bruce Eckel (original Java example)
* @author Miguel P. Monteiro (adaptation to OT/J)
*/
public team class ObservingClose extends ObserverPattern {
protected class Subject playedBy Flower
{
// This method binding uses a private field of its base class: isOpen
@SuppressWarnings("decapsulation")
changeOp <- before close
base when (base.isOpen);
}
public class BeeObserver extends Observer playedBy Bee {
update -> goToBed;
}
public class HummingbirdObserver extends Observer playedBy Hummingbird {
update -> bedTime;
}
// This and the following signature applies OTJLD §2.3.2(e):
public <AnyBase base Observer>
void mapSubject2Observer(Flower as Subject subject, AnyBase as Observer observer) {
subject.addObserver(observer);
}
public <AnyBase base Observer>
void removeObserverFromSubject(Flower as Subject subject, AnyBase as Observer observer) {
subject.removeObserver(observer);
}
public void removeAllObserversFromSubject(Flower as Subject subject) {
subject.removeAllObservers();
}
}
Differences to ObservingOpen
soon