[2]
DejaVU Online:
Principles of Object-Oriented Software Development
(©)
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.
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
th = new centigrade(); th = new fahrenheit(); th.set(f); f = th.get();
class thermometer { thermometer
protected thermometer( float v ) { temp = v; }
public void set(float v) { temp = v; }
public float get() { return temp; }
protected float temp;
};
class centigrade extends thermometer { centigrade
public centigrade() { super(0); }
public void set(float v) { temp = v + 273; }
public float get() { return temp - 273; }
};
class fahrenheit extends thermometer { fahrenheit
public fahrenheit() { super(0); }
public void set(float v) { temp = (v - 32) * 5/9 + 273; }
public float get() { return temp * 9/5 + 32 - 273; }
};
class displayer extends window { displayer
public displayer() { ... }
public void put(String s) { ... }
public void put(float f) { ... }
};
class prompter extends window { prompter
public prompter(String text) { ... }
public float get() { ... }
public String gets() { ... }
};
abstract class event { event
pubic void dependent(event e) { ... }
pubic void process() { ... }
public void operator(); // abstract method
private event[] dep;
};
class update extends event { update
public update(thermometer th, prompter p) {
_th =th; _p = p;
}
void operator()() {
_th.set( _p.get() );
process();
}
thermometer _th;
prompter _p;
};
class show extends event { show
public show(thermometer th, displayer d) {
_th = th; _d = d;
}
public void operator() {
_d.put( _th.get() );
process();
}
thermometer _th;
displayer _d;
};
thermometer c = new centigrade();
thermometer f = new fahrenheit();
displayer cd = new displayer("centigrade");
displayer fd = new displayer("fahrenheit");
prompter cp = new prompter("enter centigrade value");
prompter fp = new prompter("enter fahrenheit value");
show sc = new show(c,cd);
show sf = new show(f,fd);
update uc = new update(c,cp);
update uf = new update(f,fp);
uc.dependent(sc); uc.dependent(sf); uf.dependent(sc); uf.dependent(sf);
menu.insert(uc); menu.insert(uf);
|
Hush Online Technology
hush@cs.vu.nl
12/29/99 |
|
|