Meaningless Drivel is fun!
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
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

Design Question - Method with only Generic return type

saloni Bachchan

Joined: Jun 08, 2012
Posts: 12

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 .

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


Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

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

Joined: Dec 08, 2010
Posts: 1545

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.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
I agree. Here's the link:
subject: Design Question - Method with only Generic return type
It's not a secret anymore!