In your opinion what is the best way to construct an object given the two examples below:
Option 1: Lots of parameters
private string firstname;
private string surname;
private Address homeAddress;
private PhoneNumber homeNumber;
public Person(string firstname, string surname, Address homeAddress,
PhoneNumber homeNumber)
{
_firstname = firstname;
_surname = surname;
_homeAddress = homeAddress;
_homeNumber = homeNumber;
}
public string getFirstname()
{
return firstname;
}
public string getSurname()
{
return surname;
}
public Address getAddress()
{
return address;
}
public PhoneNumber getHomeNumber()
{
return _homeNumber;
}
Option 2: no parameters and use of set methods
private string firstname;
private string surname;
private Address homeAddress;
private PhoneNumber homeNumber;
public Person()
{
}
public string getFirstname()
{
return firstname;
}
public void setFirstname(String value)
{
firstname = value;
}
public string getSurname()
{
return surname;
}
public void setSurname(String value)
{
surname = value;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address value)
{
address = value;
}
public PhoneNumber getHomeNumber()
{
return _homeNumber;
}
public void setHomeNumber(PhoneNumber value)
{
_homeNumber = value;
}
Which method do you prefer? In my mind, option 1 is better because you know that the state of your object cannot be changed. I believe this is called immutability and is something that should be desired? On the other hand, it can quickly get out of hand and ugly if you have more than a handful of parameters.
I am aware that some languages such as C# have nice features that would probably make option 1 more appealing such as named and optional parameters however this question was prompted while dealing with Java code.
Update: Thank you for all of your suggestions. I will comment on each answer individually but a few answers suggested using a factory method instead of having the constructor do all of the work. This was actually the pattern I was using because it is a standard at work but I've never really seen the advantage of it as opposed to using the constructor to do the work. This is more how the code looked:
private string firstname;
private string surname;
private Address homeAddress;
private PhoneNumber homeNumber;
private Person()
{
}
public Person factoryCreate(string firstname, string surname, Address homeAddress,
PhoneNumber homeNumber)
{
Person person = new Person();
person.setFirstname(firstname);
person.setSurname(surname);
person.setHomeAddress(homeAddress);
person.setHomeNumber(homeNumber);
return person;
}
public string getFirstname()
{
return firstname;
}
public void setFirstname(String value)
{
firstname = value;
}
public string getSurname()
{
return surname;
}
public void setSurname(String value)
{
surname = value;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address value)
{
address = value;
}
public PhoneNumber getHomeNumber()
{
return _homeNumber;
}
public void setHomeNumber(PhoneNumber value)
{
_homeNumber = value;
}
From the discussion about having constructors do little work and using builders, I can see why having the factory method is a good one. My understanding of factory methods was that they were used to create a class given a few options. For example, if you have an Employee
class and that is sub-classed by FullTimeEmployee
and PartTimeEmployee
, you would have a factory()
method on Employee
to deal with creating the more concrete class.