aspose file tools*
The moose likes Beginning Java and the fly likes instanceof vs... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instanceof vs... " Watch "instanceof vs... " New topic
Author

instanceof vs...

Brian Spindler
Greenhorn

Joined: May 17, 2007
Posts: 29
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: May 17, 2007
Posts: 29
yeah, I understand. unfortunately I'm consuming Events from an API which I have no control over.
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26

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

Joined: Jan 28, 2003
Posts: 4179
    
  21

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.


Steve
Brian Spindler
Greenhorn

Joined: May 17, 2007
Posts: 29
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

Joined: Oct 14, 2002
Posts: 8815
    
    5
man, you guys were skating mighty close to intermediate territory


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instanceof vs...