Tag Archives: telescopic constructor pattern

Item 2. Builder Pattern, When to use?

Lets talk a bit about -

Telescoping Constructor Pattern

I have often seen code where entities are flooded with constructors. These constructors are basically overloaded for several initiations at various different invocations/constructions. This is actually the implementation of telescoping constructors pattern.

/**
 * Builder implementation of immutable class
 *
 * @author lekhak.tumblr.com
 */
public class CellPhone {
        private String companyName ;

        private String modelNo ;

        private Set hardwareList;

        private boolean isCDMA;

        private boolean isQWERTY;

        private ScreenSize screenSize;

        public CellPhone(String companyName, String modelNo, Set hardwareList) {
              this.companyName = companyName;
              this.modelNo = modelNo;
              this.hardwareList = hardwareList;
       }
       
        public CellPhone(String companyName, String modelNo, Set hardwareList, boolean isCDMA) {
              this.companyName = companyName;
              this.modelNo = modelNo;
              this.hardwareList = hardwareList;
              this.isCDMA = isCDMA;
       }

        public CellPhone(String companyName, String modelNo, Set hardwareList, boolean isCDMA, boolean isQWERTY) {
              this.companyName = companyName;
              this.modelNo = modelNo;
              this.hardwareList = hardwareList;
              this.isCDMA = isCDMA;
              this.isQWERTY = isQWERTY;
       }
}

JavaBeans Pattern

Here, we don’t do much with the constructor, but we fully depend upon the setters to populate the object with data.

Lets go through the advantages and disadvantages of the two design patterns.

/**
 * Java Beans Design Pattern
 *
 * @author lekhak.tumblr.com
 */
public class CellPhone {
        private String companyName ;

        private String modelNo ;

        private Set hardwareList ;

        private boolean isCDMA ;

        private boolean isQWERTY ;

        private ScreenSize screenSize ;

        public CellPhone() {
       }

        public String getCompanyName() {
              return companyName ;
       }

        public void setCompanyName(String companyName) {
              this.companyName = companyName;
       }

        public String getModelNo() {
              return modelNo ;
       }

        public void setModelNo(String modelNo) {
              this.modelNo = modelNo;
       }

        public Set getHardwareList() {
              return hardwareList ;
       }

        public void setHardwareList(Set hardwareList) {
              this.hardwareList = hardwareList;
       }

        public boolean isCDMA() {
              return isCDMA ;
       }

        public void setCDMA(boolean isCDMA) {
              this.isCDMA = isCDMA;
       }

        public boolean isQWERTY() {
              return isQWERTY ;
       }

        public void setQWERTY(boolean isQWERTY) {
              this.isQWERTY = isQWERTY;
       }

        public ScreenSize getScreenSize() {
              return screenSize ;
       }

        public void setScreenSize(ScreenSize screenSize) {
              this.screenSize = screenSize;
       }
}

Advantages-

  1. More than one option for initializing the object.
  2. Takes advantage of the method overloading concept of OOP.

Disadvantages-

  1. Constructors are hard to write when there are more parameters and constructors.
  2. In java beans pattern, the constructor is merely an initiator and is not responsible for filling in the data. So, this now is the responsibility of the programmer to ensure that all the required data is being filled in. This could be a little risky.
  3. Setters will prevent from the object from being immutable.

Solution: Builder Pattern

Builder pattern is widely known for its step by step completion of the object. e.g. The construction of a building doesn’t happen in one go. It may take time for a house to build.

/**
 * Builder implementation of immutable class
 *
 * @author lekhak.tumblr.com
 */
public class CellPhone {
        private String companyName ;

        private String modelNo ;

        private Set hardwareList;

        private boolean isCDMA;

        private boolean isQWERTY;

        private ScreenSize screenSize;

        public static class Builder {
              // Required Fields
              private final String companyName ;
              private final String modelNo ;
              private final Set hardwareList;

              // Optional Fields
              private boolean isCDMA ;
              private boolean isQWERTY ;
              private ScreenSize screenSize ;

              public Builder(String companyName, String modelNo,
                           Set hardwareList) {
                     this.companyName = companyName;
                     this.modelNo = modelNo;
                     this.hardwareList = hardwareList;
             }

              public Builder setIsCDMA(boolean isCDMA) {
                     this.isCDMA = isCDMA;
                     return this ;
             }

              public Builder setIsQWERTY(boolean isQWERTY) {
                     this.isQWERTY = isQWERTY;
                     return this ;
             }

              public Builder setScreenSize(ScreenSize screenSize) {
                     this.screenSize = screenSize;
                     return this ;
             }

              public CellPhone builder() {
                     return new CellPhone(this);
             }
       }

        private CellPhone(Builder builder) {
              this.companyName = builder.companyName;
              this.modelNo = builder.modelNo;
              this.hardwareList = builder.hardwareList;
              this.isCDMA = builder.isCDMA;
              this.isQWERTY = builder.isQWERTY;
              this.screenSize = builder.screenSize;
       }

}

The given code will ensure the creation of the object with the required mandatory fields. And also the other fields can be updated later.

Interaction with the builder -

CellPhone cellPhone = new CellPhone.Builder("companyName", "model33v",
    null).setIsCDMA(true).setIsQWERTY(false).setScreenSize(null).builder();

Follow

Get every new post delivered to your Inbox.

Join 74 other followers