• 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

Design Question - Method with only Generic return type

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may take a look at Factory Pattern.

Also, you should use switch statement when choosing between enum values.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic