17 Temmuz 2011 Pazar

Telescoping Constructors

Lets say we have a entity class with a lot of fields. Some are mandatory while the rest are not. It would be very hard to write constructors for every combination. So we need to implement telescoping constructor pattern with Singleton. Senior developers are very familiar with this.
Here we have our class:

==============================================================
import java.util.Date;

/**
*
* @author Burhan ARAS
*/
public class Person {
//mandatory fields
private String fullname;
private String emailaddress;
//optional fields
private String address1;
private String country;
private String town;
private String gsm;
private String birthplace;
private Date birthdate;
private String gender;
private String contact;

public Person(String fullname, String emailaddress) {
this.fullname = fullname;
this.emailaddress = emailaddress;
}



public String getAddress1() {
return address1;
}

public void setAddress1(String address1) {
this.address1 = address1;
}

public Date getBirthdate() {
return birthdate;
}

public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}

public String getBirthplace() {
return birthplace;
}

public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}

public String getContact() {
return contact;
}

public void setContact(String contact) {
this.contact = contact;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getEmailaddress() {
return emailaddress;
}

public void setEmailaddress(String emailaddress) {
this.emailaddress = emailaddress;
}

public String getFullname() {
return fullname;
}

public void setFullname(String fullname) {
this.fullname = fullname;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getGsm() {
return gsm;
}

public void setGsm(String gsm) {
this.gsm = gsm;
}

public String getTown() {
return town;
}

public void setTown(String town) {
this.town = town;
}

@Override
public String toString() {
return "Person{" + "fullname=" + fullname + ", emailaddress=" + emailaddress + ", address1=" + address1 + ", country=" + country + ", town=" + town + ", gsm=" + gsm + ", birthplace=" + birthplace + ", birthdate=" + birthdate + ", gender=" + gender + ", contact=" + contact + '}';
}

public String toStringShort(){
return "Person{" + "fullname=" + fullname + ", emailaddress=" + emailaddress + '}';
}

public static class Builder{



private Person p;
public Builder(String fullname, String emailaddress) {
p = new Person(fullname, emailaddress);
}

public Builder contact(String str) {
p.setContact(str);
return this;
}

public Builder gsm(String str) {
p.setGsm(str);
return this;
}
public Builder email(String str) {
p.setEmailaddress(str);
return this;
}
public Builder birthplace(String str) {
p.setBirthplace(str);
return this;
}
public Builder birthdate(Date date) {
p.setBirthdate(date);
return this;
}
public Builder gender(String str) {
p.setGender(str);
return this;
}
public Builder address(String str) {
p.setAddress1(str);
return this;
}
public Builder country(String str) {
p.setCountry(str);
return this;
}
public Builder town(String str) {
p.setTown(str);
return this;
}
public Person build(){
return p;
}
}

}
======================================================================

And now, we can instancate a Person like this :

Person p = new Person.Builder("isi", "syss").contact("mail").address("adrs").country("").town("").gsm("").birthplace("").birthdate(new Date()).gender("").contact("").build();


have fun guys!!