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/ObservingOpen
< OTExample Observer
Revision as of 07:38, 23 February 2010 by Unnamed Poltroon (Talk) (New page: '''Application of the Observer pattern''' This team binds the ObserverPattern: * The Subject role is played by Flower ...)
Application of the Observer pattern
This team binds the ObserverPattern:
- 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 ObservingOpen extends ObserverPattern {
protected class Subject playedBy Flower
{
// This method implicitly uses a private field of its base class: isOpen
@Override
@SuppressWarnings({ "inferredcallout", "decapsulation" })
public void changeOp() {
// manual implementation, see ObservingClose.Subject for an abbreviation.
if(!isOpen)
tsuper.changeOp();
}
changeOp <- before open;
}
public class BeeObserver extends Observer playedBy Bee {
update -> haveBreakfast;
}
public class HummingbirdObserver extends Observer playedBy Hummingbird {
update -> breakfastTime;
}
// 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();
}
}
Explanation: soon