• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design Question - Method with only Generic return type

 
saloni Bachchan
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may take a look at Factory Pattern.

Also, you should use switch statement when choosing between enum values.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic