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.
Difference between revisions of "OTExample Observer/ObservingClose"
(New page: '''Application of the Observer pattern''' This team is a slight variation of ObservingOpen, also binding the [[OTExample Observer/ObserverPattern|Obse...) |
|||
Line 1: | Line 1: | ||
− | + | ===Application of the Observer pattern=== | |
This team is a slight variation of [[OTExample Observer/ObservingOpen|ObservingOpen]], also binding the [[OTExample Observer/ObserverPattern|ObserverPattern]] like this: | This team is a slight variation of [[OTExample Observer/ObservingOpen|ObservingOpen]], also binding the [[OTExample Observer/ObserverPattern|ObserverPattern]] like this: | ||
Line 50: | Line 50: | ||
</source> | </source> | ||
− | + | ====Differences to [[OTExample Observer/ObservingOpen|ObservingOpen]]==== | |
+ | This team class is structurally equivalent to [[OTExample Observer/ObservingOpen|ObservingOpen]] with one exception: instead of overriding ''changeOp'' for conditional behaviour, we've extracted the conditional out off the method and attached it as a '''guard predicate''' to the callin binding (see line 15, {{otjld|5|.4}}). | ||
− | '' | + | Attaching a guard predicate to a callin binding is used for filtering triggers: only when the guard evaluates to true the callin binding actually fires. By adding the modifier '''base''' we say that the guard is evaluated before actually entering the team. This has two consequences: (1) the guard is evaluated in the scope of the base object (denoted by '''base''' in the expression) granting direct access to all methods and fields of the base object. (2) the guard is evaluated before the ''Flower'' instance is lifted to its ''Subject'' role. |
Latest revision as of 05:04, 24 February 2010
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
This team class is structurally equivalent to ObservingOpen with one exception: instead of overriding changeOp for conditional behaviour, we've extracted the conditional out off the method and attached it as a guard predicate to the callin binding (see line 15, OTJLD §5.4).
Attaching a guard predicate to a callin binding is used for filtering triggers: only when the guard evaluates to true the callin binding actually fires. By adding the modifier base we say that the guard is evaluated before actually entering the team. This has two consequences: (1) the guard is evaluated in the scope of the base object (denoted by base in the expression) granting direct access to all methods and fields of the base object. (2) the guard is evaluated before the Flower instance is lifted to its Subject role.