tags:

views:

47

answers:

5

Hi,

I need to add multiple entries of my PhoneNumber(number,type) to my main Person(name,PhoneNumber) object.

Can I use varargs for working this out or is there a better and more efficient way to do it??

I am trying to work it as follows:

// number is a String and type is an enum

PhoneNumber [] numbers = {

    new PhoneNumber(number , type),
    new PhoneNumber(number,type),
    new PhoneNumber(number,type),

};

class Person(){

 Person(String name, PhoneNumber...numbers){

  // adding all the phonenumber objects to a list for each person.
}

I am not sure if this is the correct way to implement it and whether varargs is the best option?

Please advise. I know its a basic java question but was not finding a better solution to above problem.

+1  A: 

It's not a bad option. I would add another constructor to handle Collections too.

The only thing I'm not sure with your code is the fact that the constructor should handle or not PhoneNumbers but since you're doing things this way, I suppose you have good reasons.

Colin Hebert
+1 this was my first thought.
Tony Ennis
+1  A: 

The question to ask is:

Can a Person be a valid object if it has zero PhoneNumbers?

If so, remove PhoneNumber from the constructor and create

Person::addPhone(PhoneNumber phone) { ... }

Then you don't need varadic constructors. If a Person cannot be valid without a number then passing a variable sized list of PhoneNumbers is far more clear.

By "valid" I mean can the object be logically consistent. Of course you couldn't Person::SendSMS on an object that has no phone number, but I myself could have no contact number and still be a valid entity that you want to track.

msw
+1 Another good option. The easier the class is to construct the easier it is to test.
Tony Ennis
A: 

I think it should be ok, But you don't need to construct an array of phone numbers, since you have varargs as a param in Person's constructor.

You can directly call Person's constructor like this.

Person p = new Person("JOHN", new PhoneNumber(numbertype.HOME, "123"),
                new PhoneNumber(numbertype.OFFICE, "456"), new PhoneNumber(
                        numbertype.OTHER, "789"));
johnbk
You answered the question, which is cool, but I'd never consider implementing it this way.
Tony Ennis
why?? what do you suggest to be better then?
jillika iyer
@Toy Ennis - I have the same question as @jillka iyer
johnbk
I would not too, its not clear and simple in my eyes.
InsertNickHere
A: 

I would simply seperate the phone part. Like

class Person(){

 Person(String name){
 }

 void addPhoneNumbers(PhoneNumber...numbers) {
 }
}
InsertNickHere
A: 

I wonder, where do these phone numbers come from ? It's unlikely that those phone numbers will appear just right in the source code. I mean, do you really think that someone will actually write something like new Person("arthur", phoneNo1, phoneNo2, phoneNo3), in a real life code ?

I don't think so (okay, such a code can appear in a unit-test, but not in a 'real' code). Those informations will come from either a configuration file, or an action from the user, or a stream from a network, anything. In any case, the phone number will come one by one, or in some kind of Collection. So, I suggest you to have a method addPhoneNumber(PhoneNumber n), and optionaly a method addAllPhoneNumbers(Collection<PhoneNumner> ns).

I suggest that the latter method be added only at the time you need it, or if you know for sure it will be useful. This is just a convenient method, after all.

I think varargs are useful only when a developper is likely to write the method call by hand (String.format is an appropriate use of varargs).

barjak
thanks :) ended up doing it like this! seemed better when i wanted to add multiple phone numbers :)
jillika iyer