Reputation: 2849
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.
Upvotes: 2
Views: 517
Reputation: 11270
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).
Upvotes: 0
Reputation: 3666
I would simply seperate the phone part. Like
class Person(){
Person(String name){
}
void addPhoneNumbers(PhoneNumber...numbers) {
}
}
Upvotes: 0
Reputation: 3252
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"));
Upvotes: 0
Reputation: 43497
The question to ask is:
Can a
Person
be a valid object if it has zeroPhoneNumber
s?
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 PhoneNumber
s 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.
Upvotes: 2
Reputation: 93177
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.
Upvotes: 1