This will be in C++11, but the object design should be rather agnostic.
I have 3 interface classes, IEvent
, ICondition
, IRunnable
.
The main loop processes multiple kinds of IEvent
, ranging from "an Application has launched" to "User clicked position (x,y)". Then for each type of event, a list of ICondition
are notified through a listener interface. For example, ConditionUserClickedPos
implements IEventClickListener::onClick(int x, int y)
, which is called by the main loop.
If the position is within the conditions, one or more IRunnable
objects are called through IRunnable::execute(IEvent triggeringEvent)
, like class RunnableDrawCircleAtClickPosition
and class RunnableShowPrettyWindow
.
My problem is as follow :
Within IRunnable::execute(IEvent triggeringEvent)
implementation, I need to get the precise information of the event, but I don't know the exact class hierarchy of the triggeringEvent.
This can be easily solved with dynamic_cast, checking typeid or other reflective oop methods, but those usually breed rather smelly code. I'm also trying to keep the code extendable for new and unrelated events, conditions and runnables that I don't know about yet.
I could also declare a bunch of overloads for ::execute(...)
based on children class types, but this doesn't seem right either.
Is there a silver bullet that I've missed here?
if(event.type === ...)
is much more palatable than astd::dynamic_cast
, at least for me.