This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Design Question - Method with only Generic return type 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 » Java in General
Bookmark "Design Question - Method with only Generic return type" Watch "Design Question - Method with only Generic return type" New topic
Author

Design Question - Method with only Generic return type

saloni Bachchan
Greenhorn

Joined: Jun 08, 2012
Posts: 12
Hi,

I need some help with Java Generic Programming.

I am implementing a method that takes in some parameters and based on the parameters ,it returns a list of objects.
Now the class of these objects depends on the enum argument passed into the method.
here's my method: The parameter RiskShape is an Enum.



I want to know if it is a good idea to do this?Or is this a bad design?
Note that the code that calls the above method will also have to check for the type of objects in the list .
So



I am not sure if this is a good way to do things.
If not ,can someone please suggest a better approach.

Thanks!

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You may take a look at Factory Pattern.

Also, you should use switch statement when choosing between enum values.


The quieter you are, the more you are able to hear.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi saloni,

Welcome to CodeRanch!

Please, UseCodeTags. I've updated your post for now, but please keep this in mind during further posts.

Coming to your question, no offenses, but I don't like the idea of checking same condition again (i.e. in requestSnapshot, and outside of that method).

I would do it like:


However, please be careful while writing code for requestSnapshot. If you are only returning ArrayList of non-generic types (like A or B), then how about below:


and then

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design Question - Method with only Generic return type