Create Builder Pattern in Java with Example

[1515 views]




What is Builder Pattern?

Builder Pattern is used for creating Complex object using simple objects by using a step by step approach. This is a part of Creational Pattern as this pattern provides a very good way to create an object.

Let's see an example of how we can create our own Builder Pattern in Java.


Car.java

package com.example.model; public class Car { private String chassis; private String body; private String paint; private String interior; public Car() { super(); } public Car(String chassis, String body, String paint, String interior) { this(); this.chassis = chassis; this.body = body; this.paint = paint; this.interior = interior; } public String getChassis() { return chassis; } public void setChassis(String chassis) { this.chassis = chassis; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getPaint() { return paint; } public void setPaint(String paint) { this.paint = paint; } public String getInterior() { return interior; } public void setInterior(String interior) { this.interior = interior; } public boolean doQualityCheck() { return (chassis != null && !chassis.trim().isEmpty()) && (body != null && !body.trim().isEmpty()) && (paint != null && !paint.trim().isEmpty()) && (interior != null && !interior.trim().isEmpty()); } @Override public String toString() { // StringBuilder class also uses Builder Design Pattern with implementation of java.lang.Appendable interface StringBuilder builder = new StringBuilder(); builder.append("Car [chassis=").append(chassis).append(", body=").append(body).append(", paint=").append(paint) .append(", interior=").append(interior).append("]"); return builder.toString(); } }

CarBuilder.java

package com.example.model; public interface CarBuilder { // Step 1 public CarBuilder fixChassis(); // Step 2 public CarBuilder fixBody(); // Step 3 public CarBuilder paint(); // Step 4 public CarBuilder fixInterior(); // delivery of car public Car build(); }


SportsCarBuilder.java

package com.example.model; public class SportsCarBuilder implements CarBuilder { private String chassis; private String body; private String paint; private String interior; public SportsCarBuilder() { super(); } @Override public CarBuilder fixChassis() { System.out.println("Assembling chassis of the sports model"); this.chassis = "Sporty Chassis"; return this; } @Override public CarBuilder fixBody() { System.out.println("Assembling body of the sports model"); this.body = "Sporty Body"; return this; } @Override public CarBuilder paint() { System.out.println("Painting body of the sports model"); this.paint = "Sporty Torch Red Paint"; return this; } @Override public CarBuilder fixInterior() { System.out.println("Setting up interior of the sports model"); this.interior = "Sporty interior"; return this; } @Override public Car build() { Car car = new Car(chassis, body, paint, interior); if (car.doQualityCheck()) { return car; } else { System.out.println("Car assembly is incomplete. Can't deliver!"); } return null; } }


Main.java

package com.example; public class Main { public static void main(String[] args) { CarBuilder builder = new SportsCarBuilder(); Car car = builder.fixChassis().fixBody().paint().fixInterior().build() if (car != null) { System.out.println("Below car delievered: "); System.out.println(car); } } }

In above code, Car.java is our POJO class for which we want to create a builder pattern. CarBuilder.java is the Builder Interface containing basic method definitions we want our builder to have. SportsCarBuilder provides an implementation for CarBuilder Class describing it as Sports Car. You can create any such Builder implementations as per your need, like FuelEfficientBuilder or ElectricCarBuilder.

Now, for creating an object using Builder pattern, refer "Main.java" class. In that we are creating an instance of SportsCarBuilder Class and then use chain of methods defined in SportsCarBuilder Class to create a Car Object.

I hope you understood how to create your custom Builder Pattern as per your need in your Java Application.

                  Liked Article? Please Buy me a Coffee 😭



Want to Clear Java Interview? Read Java Interview Notes prepared by Us


Are You Good enough in Java?



Learn Python in a Week and Master It

Comments








Search
Need Programming Help??


Below Offers ends in













Quiz For You:

Search Tags

    Create Builder Pattern using Java

    Simple Java Code for builder pattern