This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
So I've got an Event listener class that has to handle events. Right now the events are received as an Array so I have :
My issue is that right now the code does something like:
What are some alternative strategies/patterns to this design? All events are certain subclass types and processing is specific per class type but sometimes the same for several (Event1 and Event2 are same but Event3 requires special handling).
Brian Spindler wrote:yeah, I understand. unfortunately I'm consuming Events from an API which I have no control over.
How little control over the API do you have? Do you have the ability to add different Event Listeners for the different event types in whatever class(es) generate the event?
I mean, ideally:
Another option would be to map event handlers to the expected class name or event type name:
This has limitations on mapping an event on a per-class type, so there would be tight coupling between the event class and the handler class, not suitable if you don't know the exact classes of the events or want to code to some parent/interface type. A better solution would be to use names for the events, if that is possible with the API.
Joined: May 17, 2007
Thanks Steve, I have NO control so I think I'm going to have to find a happy medium along the lines of your example. Thanks!