In this section we will look at the Reactor
pattern that explains the interaction between objects
and the environment.
We will also look at an event system, in
which the event types are defined by the application programmer.
In this application, events are used to maintain global
consistency, similar to the Observer pattern.
The Reactor pattern
The Reactor pattern has been introduced
in
See D.C. Schmidt, Using Design Patterns to Develop Reusable Object-oriented
Communication Software, CACM October '95, 38(10): 65-74
Concrete handlers, derived from an abstract handler,
must provide a method, such as
Consequences
The fact that control is handed over to
the environment has, however, also some disadvantages.
First of all, as experience with student assignments shows,
it is difficult to learn in the beginning.
But even when mastered, applications may be hard
to debug, since it is not always clear why a particular handler
was invoked, and because it may be difficult to repeat
the computation preceding the fault.
Applicability
Abstract event systems
To conclude this chapter about idioms and patterns,
we will look at a somewhat more detailed example
employing (user-defined) events to characterize
and control the interaction between the objects.
The example is taken from
For
draft version 0.1 (15/7/2001)Functional behavior
protected thermometer( float v ) { temp = v; }
public void set(float v) { temp = v; }
public float get() { return temp; }
protected float temp;
};
public centigrade() { super(0); }
public void set(float v) { temp = v + 273; }
public float get() { return temp - 273; }
};
public fahrenheit() { super(0); }
public void set(float v) { temp = (v - 32) * 5/9 + 273; }
public float get() { return temp * 9/5 + 32 - 273; }
};
User interface
public displayer() { ... }
public void put(String s) { ... }
public void put(float f) { ... }
};
public prompter(String text) { ... }
public float get() { ... }
public String gets() { ... }
};
Events
pubic void dependent(event e) { ... }
pubic void process() { ... }
public void operator(); // abstract method
private event[] dep;
};
public update(thermometer th, prompter p) {
_th =th; _p = p;
}
void operator()() {
_th.set( _p.get() );
process();
}
thermometer _th;
prompter _p;
};
public show(thermometer th, displayer d) {
_th = th; _d = d;
}
public void operator() {
_d.put( _th.get() );
process();
}
thermometer _th;
displayer _d;
};
The installation
Discussion
[]
readme
course
preface
1
2
3
4
5
6
7
8
9
10
11
12
appendix
lectures
resources
eliens@cs.vu.nl