• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instanceof vs...

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, I understand. unfortunately I'm consuming Events from an API which I have no control over.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
man, you guys were skating mighty close to intermediate territory
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic