0

I have a situation similar to the following:

class EventHandler { ... }; 
class Event {void Accept(EventHandler&) { ... }};

class SpecialEventHandler : public EventHandler { ... };
class SpecialEvent : public Event {void Accept(SpecialEventHandler&) { ... }};

There are some events which are a subtype of Event and some which are a subtype of SpecialEvent. These events get added to a queue of Events and then I need to handle them with a SpecialEventHandler.

The problem is handling the special events as the only the Accept method that can be called is the one in Event so only an EventHandler can be passed in. I have a solution using a double visitor pattern but this is clunky and confusing.

  • Is there a pattern for this kind of situation?
  • Is there a better way to design it?
DrYap
  • 6,525
  • 2
  • 31
  • 54
  • Can you have two different queues? – Peter Wood Mar 29 '14 at 21:36
  • That is possible but would add complexity with timing issues to get the order of events right. Also there may be different types of event added in future. – DrYap Mar 29 '14 at 22:08
  • 1
    Add an empty virtual `Accept()` to `Event`, presumably you'll need it anyway for the other `Event` derivees. "I need to call something depending on the class" is best handled by the class itself, across the board. – vonbrand Mar 30 '14 at 02:44
  • The `Event` doesn't know how it should be handled, that depends on the event, handler subclass and the implementation of the handler which is why I see it as triple dispatch. – DrYap Mar 30 '14 at 07:30
  • I'm not clear on why you need `SpecialEvent`, etc. The Visitor pattern is appropriate in static languages for relatively stable hierarchies, as all the derived types need to be known by the base `Event`. For dynamic, more reflective languages, this restriction is removed. – Peter Wood Mar 30 '14 at 17:49
  • The direct subtypes of `Event` are part of the public API and the `SpecialEvent`s are part of the private API which should be hidden. – DrYap Mar 30 '14 at 18:53

0 Answers0