Builder design pattern


This is another creational design pattern. Just think about the StringBuilder. If you create a StringBuilder object, you can append strings one by one to the same StringBuilder object.

                StringBuilder stringBuilder = new StringBuilder();
       stringBuilder.append("Java").append("Foundation");


This is the functionality of the builder pattern. There are a few ways to implement the builder pattern.

Using constructor


package com.app.designPatterns.builder1;
public class BuilderApp {
public static void main(String[] args) {
Phone iphone = new PhoneBuilder().setModel("Apple").setOs("IOS").buildPhone();
System.out.println(iphone);
Phone androidPhone = new PhoneBuilder().setModel("Nokia").setOs("Android").setCamera("32MP").buildPhone();
System.out.println(androidPhone);
}
}
package com.app.designPatterns.builder1;
public class Phone {
private String model;
private String os;
private Integer price;
private String camera;
public Phone(String model, String os, Integer price, String camera) {
super();
this.model = model;
this.os = os;
this.price = price;
this.camera = camera;
}
@Override
public String toString() {
return "Phone{" +
"model='" + model + '\'' +
", os='" + os + '\'' +
", price=" + price +
", camera='" + camera + '\'' +
'}';
}
}
view rawPhone.java hosted with ❤ by GitHub
package com.app.designPatterns.builder1;
public class PhoneBuilder {
private String model;
private String os;
private Integer price;
private String camera;
public PhoneBuilder setModel(String model) {
this.model = model;
return this;
}
public PhoneBuilder setOs(String os) {
this.os = os;
return this;
}
public PhoneBuilder setPrice(Integer price) {
this.price = price;
return this;
}
public PhoneBuilder setCamera(String camera) {
this.camera = camera;
return this;
}
public Phone buildPhone() {
return new Phone(model, os, price, camera);
}
}


Using inner class

package com.app.designPatterns.builder3;
public class Car {
private final String make;
private final Integer price;
private final Boolean sunroof;
private final Boolean alloyWheels;
private final Boolean cruiseControl;
private final Boolean electricSeats;
public Car(CarBuilder carBuilder){
this.make = carBuilder.make;
this.price = carBuilder.price;
this.sunroof = carBuilder.sunroof;
this.alloyWheels = carBuilder.alloyWheels;
this.cruiseControl = carBuilder.cruiseControl;
this.electricSeats = carBuilder.electricSeats;
}
static class CarBuilder{
private String make;
private Integer price;
private Boolean sunroof;
private Boolean alloyWheels;
private Boolean cruiseControl;
private Boolean electricSeats;
public Car build(){
return new Car(this);
}
public CarBuilder(String make){
this.make = make;
}
public CarBuilder price(Integer price){
this.price = price;
return this;
}
public CarBuilder sunroof(Boolean sunroof){
this.sunroof = sunroof;
return this;
}
public CarBuilder alloyWheels(Boolean alloyWheels){
this.alloyWheels = alloyWheels;
return this;
}
public CarBuilder cruiseControl(Boolean cruiseControl){
this.cruiseControl = cruiseControl;
return this;
}
public CarBuilder electricSeats(Boolean electricSeats){
this.electricSeats = electricSeats;
return this;
}
}
@Override
public String toString() {
return "Car{" +
"make='" + make + '\'' +
", price=" + price +
", sunroof='" + sunroof + '\'' +
", alloyWheels='" + alloyWheels + '\'' +
", cruiseControl='" + cruiseControl + '\'' +
", electricSeats='" + electricSeats + '\'' +
'}';
}
}
view rawCar.java hosted with ❤ by GitHub
package com.app.designPatterns.builder3;
public class CarApp {
public static void main(String[] args) {
Car.CarBuilder priusBuilder = new Car.CarBuilder("Prius");
Car prius = priusBuilder.price(200).sunroof(true).alloyWheels(true).cruiseControl(true).electricSeats(true).build();
System.out.println(prius);
Car.CarBuilder priusCBuilder = new Car.CarBuilder("Prius C");
Car priusC = priusCBuilder.price(180).sunroof(true).alloyWheels(true).cruiseControl(false).build();
System.out.println(priusC);
Car.CarBuilder priusVBuilder = new Car.CarBuilder("Prius V");
Car priusV = priusVBuilder.price(180).alloyWheels(true).build();
System.out.println(priusV);
}
}
view rawCarApp.java hosted with ❤ by GitHub

The second method is the best way to implement the builder pattern. 

Advantages of the Builder pattern

  • The builder pattern solves the complex Telescopic constructors
  • Make it easy to create objects that have many attributes.

Disadvantages of the Builder pattern

  • Code looks complex
  • Object creation depends on the Builder class

Use of Builder design pattern in Java core

  • StringBuilder
  • StringBuffer
  • JsonObjectBuilder
  • Calendar in Java 8
  • Locale in Java 7
  • Stream API in Java 8

Builder design pattern Builder design pattern Reviewed by Ravi Yasas on 8:45 PM Rating: 5

No comments:

Powered by Blogger.