27 Apr 2015

Builder Design Pattern in Java with Example

Builder Design Pattern is a Creational Design Pattern which provides a way to create a complex object by using small objects through step-by-step procedure and returns the final object.

Let us see how to implement this design pattern with an example.
public class Computer{
   // Required Parameters
   private String HDD;
   private String RAM;
 
   // Optional Parameters
   private boolean isBlueetoothEnabled;
   private boolean isGraphicsCardEnabled;
 
   // Getters
   public String getHDD(){
       return HDD;
   }
 
   public String getRAM(){
       return RAM;
   }
 
   public boolean getIsBlueetoothEnabled(){
       return isBlueetoothEnabled;
   }
 
   public boolean getIsGraphicsCardEnabled(){
       return isGraphicsCardEnabled;
   }
 
   private Computer(ComputerBuilder builder){
       this.HDD = builder.HDD;
       this.RAM = builder.RAM;
       this.isBlueetoothEnabled = builder.isBlueetoothEnabled;
       this.isGraphicsCardEnabled = builder.isGraphicsCardEnabled;
   }
 
 public static class ComputerBuilder{
  // Required Parameter
  private String HDD;
  private String RAM;
  
  // Optional Parameters
  private boolean isBlueetoothEnabled;
  private boolean isGraphicsCardEnabled;
  
  public ComputerBuilder(String HDD, String RAM){
     this.HDD = HDD;
     this.RAM = RAM;
  }
  
  public ComputerBuilder setIsBlueetoothEnabled(boolean isBlueetoothEnabled){
     this.isBlueetoothEnabled = isBlueetoothEnabled;
     return this;
  }
  
  public ComputerBuilder setIsGraphicsCardEnabled(boolean isGraphicsCardEnabled){
     this.isGraphicsCardEnabled = isGraphicsCardEnabled;
     return this;
  }
 
  public Computer build(){
     return new Computer(this);
  }
 
 }
}

public class ComputerTest{
   public static void main(String args[]){
      Computer myComputer = new Computer.ComputerBuilder("500 GB", "1 GB").
            setIsBlueetoothEnabled(true).setIsGraphicsCardEnabled(false).
            build();
      System.out.println("Computer = "+myComputer.getHDD()+":"+
            myComputer.getRAM()+":"+
            myComputer.getIsBlueetoothEnabled()+":"+
            myComputer.getIsGraphicsCardEnabled());
   }
}
Consider we are trying to build a computer in steps, usually it has HDD & RAM as required things and Bluetooth & Graphic options as optional. So, initially we are creating a builder object with 500GB HDD & 1GB RAM by using static inner class 'ComputerBuilder' as '
ComputerBuilder("500 GB", "1 GB")'.
Then we are extending the features of builder object by enabling/disabling Bluetooth & Graphic options, optional.

Finally we are calling build() method of static inner class which uses the reference (this) of static inner class for creating an instance of Computer class with the given required and optional options and returns it, i.e., final object.

Thus by using builder design pattern we are creating a complex object by using small objects in steps and finally returning the final object.





Popular Posts

Write to Us
Name
Email
Message