subsections:


The Reactor pattern

See D.C. Schmidt, Using Design Patterns to Develop Reusable Object-oriented Communication Software, CACM October '95, 38(10): 65-74


slide: The Reactor pattern



slide: The Reactor pattern -- structure



slide: The Reactor Pattern - interaction



  th = new centigrade();
  th = new fahrenheit();
  th.set(f);
  f = th.get();
  

For thermometer th, th1; float f;


slide: Abstract system -- thermometers


  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);