Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof vs...

 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

Thanks in advance!
 
Paul Clapham
Sheriff
Pie
Posts: 20756
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, ideally your code would look like this:

This would require each of your subclasses to be subclasses of Event, and to have a handle() method which does whatever is required to handle an event of that class.
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, I understand. unfortunately I'm consuming Events from an API which I have no control over.
 
Phuc Bui
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you want to have particular action by yourself with each of instance of Event, or special method which only children class have, I think you can do like that.

If you use the method which implemented by child class from parent, you do like that

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
man, you guys were skating mighty close to intermediate territory
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic