th = new centigrade(); th = new fahrenheit(); th.set(f); f = th.get();
class thermometer {protected thermometer( float v ) { temp = v; } public void set(float v) { temp = v; } public float get() { return temp; } protected float temp; };
thermometer
class centigrade extends thermometer {public centigrade() { super(0); } public void set(float v) { temp = v + 273; } public float get() { return temp - 273; } };
centigrade
class fahrenheit extends thermometer {public fahrenheit() { super(0); } public void set(float v) { temp = (v - 32) * 5/9 + 273; } public float get() { return temp * 9/5 + 32 - 273; } };
fahrenheit
class displayer extends window {public displayer() { ... } public void put(String s) { ... } public void put(float f) { ... } };
displayer
class prompter extends window {public prompter(String text) { ... } public float get() { ... } public String gets() { ... } };
prompter
abstract class event {pubic void dependent(event e) { ... } pubic void process() { ... } public void operator(); // abstract method private event[] dep; };
event
class update extends event {public update(thermometer th, prompter p) { _th =th; _p = p; } void operator()() { _th.set( _p.get() ); process(); } thermometer _th; prompter _p; };
update
class show extends event {public show(thermometer th, displayer d) { _th = th; _d = d; } public void operator() { _d.put( _th.get() ); process(); } thermometer _th; displayer _d; };
show
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);