• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

how to implement method which can return different object at different time

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

In our project I have situation where one method takes some parameters,
based on parameters(list of x kind of objects) in loop method calls database for each x object and validate details
if validation for all objects are thru method should return list of y objects,
if validation fails for one of the x object it should return validation error object( project specific object to details issue to end-user)

so here by same method in one situation we want to return list whereas in another condition we want to return single Validation object.

Important point to note here is there no common super class between List and validation object except "Object" class , which I don't should be good to return as generic return object.

please suggest how should I design method in this situation?

Mohit
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you don't want to throw an Exception, which can return error messages, etc.?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Pat, its better to throw exception in case of an error and handle it in the calling method.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohit with Josh Mehta wrote:


Please check your private messages for an important administrative matter
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are not sure of the method return type, you return generic Object in a method signature

In calling routine , find out the type of the returned object using getClass().getName() and then type cast it and then use it.

e.g.



in calling routine
 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Use code tags

2) Don't use that construct for checking the class. If you ever decide to use this (which I wouldn't in this case, I'd go for the exception) use the instanceof operator instead:
 
maitrayi vasishtha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob for suggesting use of codetags.

As I am new to this forum , it was not known to me. Sorry.

I agree with you that instead of using getClass().getName() better use instanceof operator.

Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maitrayi
I have edited your post to incorporate the tags for you.
In future you can use the to edit your posts.

You might want to check out http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch and http://faq.javaranch.com/java/HowToAnswerQuestionsOnJavaRanch for tips on how to get the most out of the Ranch.
 
maitrayi vasishtha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maneesh.

 
Bartender
Posts: 2899
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

to implement method which can return different object at different time



I believe its a bad design pattern to do that
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic