Prev
Next

Here's a better way of handling events with templates. Pretend Java has templates which use a syntax a bit less ugly than C++'s:
 1 //let's see what code2html does with this made-up language...
 2 
 3 //INTERFACE:
 4 interface EventListener <class EventTy> {
 5 public:
 6   abstract void eventPerformed (EventTy e);
 7 }
 8 
 9 //java doesn't support default arguments, either
10 interface Event <class ComponentTy = Object> {
11   public:
12   abstract ComponentTy getSource ();
13 }
14 
15 interface EventGenerator <class EventTy> {
16 	abstract void addEventListener (EventListener <EventTy>);
17 	abstract void removeEventListener (EventListener <EventTy>);
18 }
19 
20 class EventMulticaster <class EventTy> implements EventListener <EventTy> {
21     //insert 75 lines of code here
22     //But that's now Sun's job, not mine
23 }
24 
25 //USER CODE:
26 class MyEventListener implements EventListener <Event <ColorChanger>>  {
27 public:
28   void eventPerformed (Event <ColorChanger> e) {
29       ColorChanger cc = e.getSource (); //look Ma, no casting!
30     setColor (cc.getColor ()); 
31     /* ... */
32   }
33 }


Prev
Next