For a small administrative program, I have to be able to register people and their data. But, files are sometimes created through a phone call "on the fly" and then later certain data is added to the file to complete it.
For this reason I started making my Person
class with different constructors and I was chaining them, until I came to the conclusion that I probably had a bad design because each constructor was only adding one more parameter.
After some research, I implemented the Builder pattern (even found a nice example that used a Person
class as example). But I am not sure if this is the correct approach for my situation. As I read (or understood), an object created through a Builder pattern is immutable. And as I said, data might sometimes be added later after a Person
has been created already (phone number or e-mail or address, etc..).
This means I am changing my object, correct? Which is wrong to do.
Also, People
have an address, but people can always move to a new location.
For this reason, I have an editAddress()
method in my Address
class.
This also means I am changing my object after it's been created, no?
And I plan to make this Person
class my base class for 2 other types of Person
; a child and a parent. Will my builder pattern still be useful, then?
My 3 questions are:
- Is it ok to add data after the object has been created? (if not, how should I approach this?)
- Is it ok to have methods in underlying classes that change the data? (if not, how should I approach this?)
- Will this still prove useful once I start with my sub classes; child and parent?
My first code with the chained constructors
public class Person {
private String name;
private String familyName;
private String dateOfBirth;
private Address address;
private String phoneNumber;
private String eMail;
public Person(String name, String familyName) {
this.name = name;
this.familyName = familyName;
}
public Person(String name, String familyName, String dateOfBirth) {
this(name, familyName);
this.dateOfBirth = dateOfBirth;
}
public Person(String name, String familyName, String dateOfBirth, Address address) {
this(name, familyName, dateOfBirth);
this.address = address;
}
public Person(String name, String familyName, String dateOfBirth, Address address, String phoneNumber) {
this(name, familyName, dateOfBirth, address);
this.phoneNumber = phoneNumber;
}
public Person(String name, String familyName, String dateOfBirth, Address address, String phoneNumber, String eMail) {
this(name,familyName,dateOfBirth,address,phoneNumber);
this.eMail = eMail;
}
}
My code with the builder pattern
public class Person {
private final String name;
private final String familyName;
private Date dateOfBirth;
private Address address;
private String phoneNumber;
private String eMail;
public Person(PersonBuilder builder) {
this.name = builder.name;
this.familyName = builder.familyName;
this.dateOfBirth = builder.dateOfBirth;
this.address = builder.address;
this.phoneNumber = builder.phoneNumber;
this.eMail = builder.eMail;
}
@Override
public String toString() {
return String.format("%s %s\n%tD\n%s\nTel:%s\nEmail:%s",this.name,this.familyName,this.dateOfBirth,this.address,this.phoneNumber,this.eMail);
}
public static class PersonBuilder {
private final String name;
private final String familyName;
private Date dateOfBirth = new Date();
private Address address = new Address("Unknown","00","0000","Unknown");
private String phoneNumber = "0000/00 00 00";
private String eMail = "[email protected]";
public PersonBuilder(String name, String familyName) {
this.name = name;
this.familyName = familyName;
}
public PersonBuilder dateofBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
return this;
}
public PersonBuilder address(Address address) {
this.address = address;
return this;
}
public PersonBuilder phoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
return this;
}
public PersonBuilder eMail(String eMail) {
this.eMail = eMail;
return this;
}
public Person build() {
return new Person(this);
}
}
}
My editAddress
method
public void editAddress(Address address){
this.street = address.street;
this.houseNr = address.houseNr;
this.areaCode = address.areaCode;
this.city = address.city;
}