DEV Community

Ravi Yasas
Ravi Yasas

Posted on

3

Builder 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");
Enter fullscreen mode Exit fullscreen mode

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);
}
}
view raw BuilderApp.java hosted with ❤ by GitHub
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 raw Phone.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 classes

This is the best approach to implement the builder pattern.

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 raw Car.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 raw CarApp.java hosted with ❤ by GitHub

--> Find advantages, disadvantages, usages and more details about the builder pattern

Top comments (2)

Collapse
 
andsmile profile image
Andrii

the most used way to implement builder nowadays is to use lombok Builder annotation
:)))

Collapse
 
raviyasas profile image
Ravi Yasas

Yeah. that is true.