A Question about method overloading in Java

P

Prakash Prabhu

Hi,

I have a requirement where in I have to retrieve a set of data
from the database and populate a Collection class depending upon a
a criterion whose value is a String. The criterion itself can be of
three types (at present ) .Also the method takes 2 additional objects
of 2 different user-defined types in all three cases. For example, if
the method is initializeCollection() and the criteria are say A,B,C ,
then is it advisable to overload this method like :

For Criterion A :
boolean initializeCollection(Class1 obj1, Class2 obj2,String Avalue)

For Criterion B :
boolean initializeCollection(Class1 obj1, String Bvalue,Class2 obj2)

For Criterion C :
boolean initializeCollection(String Cvalue,Class1 obj1, Class2 obj2)

or have a single method and pass an additional ( int ) flag to indicate
the type of criterion passed :

boolean initializeCollection(Class1 obj1, Class2 obj2,String value,int type)

// type would be 0 for A,1 for B , 2 for C


Thanks for any pointers .

Regards,
Prakash
 
K

Kirk Knoernschild

There are two other alternatives that are clearer.

boolean initializeCollection(Class1 obj1, Class2 obj2, String value)

Just put an if statement and call private methods to populate the
Collection differently. That's very simple.

If your logic to populate the collection is very different and rather
complex, you might consider using a Strategy for value and delegate
building the collection to the Strategy.

boolean initializeCollections(Class1 obj1, Class2 obj2, SomeCriteria
criteria)

Kirk Knoernschild |
Senior Consultant/Instructor | www.kirkk.com
TeamSoft, Inc. | www.extensiblejava.com
Training, Mentoring, Consulting | www.teamsoftinc.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top