Builder design pattern

The builder pattern is an object creation software design pattern. Unlike the abstract factory pattern and the factory method pattern whose intention is to enable polymorphism, the intention of the builder pattern is to find a solution to the telescoping constructor anti-pattern citation needed that occurs when the increase of object constructor parameter combination leads to an exponential list of constructors. Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

[Wikipedia]

Intro

In this example,we will create two engineers, which will implement the Worker interface. They will be able to build a car using the CarBuilder Methods (both will use this class). Each Worker has a field of Car in his class,so he can build it while using CarPlan interface methods. 

CarPlan

public interface CarPlan {
    public void setWheels(String wheels);
    public void setGear(String gear);
    public void setEngine(String engine);
    public void setCarColor(String color);
}

Worker


//This class is known as Director at the Builder design pattern.

public interface Worker {
    public void bulidEngine();
    public void buildGear();
    public void assembleWheels();
    public void paintTheCar();
    public Car getCar();
}

Car


    public class Car implements CarPlan {
    private String wheels;
    private String gear;
    private String engine;
    private String color;

    @Override
    public void setWheels(String wheels) {
        this.wheels=wheels;
    }

    @Override
    public void setGear(String gear) {
        this.gear=gear;
    }

    @Override
    public void setEngine(String engine) {
        this.engine=engine;
    }

    @Override
    public void setCarColor(String color) {
    this.color=color;
    }
    public String toString(){
        return "A Car was made of the following parts: Engine: "+ engine + " , Wheels: " + wheels + ", Gear: " + gear + 
        " , Car color: " + color;
    }

}

CarBuilder


    public class CarBuilder {
    private Worker worker;

    public CarBuilder(Worker worker)
    {
        this.worker = worker;
    }

    public void buildACar()
    {
        this.worker.bulidEngine();
        this.worker.buildGear();
        this.worker.assembleWheels();
        this.worker.paintTheCar();
    }

    public Car getCar()
    {
        return this.worker.getCar();
    }
}
    
    

MustangWorker


    public class MustangWorker implements Worker {
    private Car car;
    private String wheels;
    private String gear;
    private String engine;
    private String color;

    public MustangWorker(){
        this.car=new Car();
    }
    @Override
    public void bulidEngine() {
        car.setEngine("Mustang D-code, 5.0L V8");
    }

    @Override
    public void buildGear() {
        car.setGear("Manual");
    }

    @Override
    public void assembleWheels() {
        car.setWheels("Silver AMR Wheels 2018");
    }

    @Override
    public void paintTheCar() {
        car.setCarColor("Red");
    }

    @Override
    public Car getCar() {
        return this.car;
    }
}

MazdaWorker



public class MazdaWorker implements Worker {
    private Car car;
    private String wheels;
    private String gear;
    private String engine;
    private String color;

    public MazdaWorker(){
        this.car=new Car();
    }
    @Override
    public void bulidEngine() {
        car.setEngine("Mazda Engn 3.5L, V6");
    }

    @Override
    public void buildGear() {
        car.setGear("Automatic");
    }

    @Override
    public void assembleWheels() {
        car.setWheels("Black TBS Wheels 1997");
    }

    @Override
    public void paintTheCar() {
        car.setCarColor("Green");
    }

    @Override
    public Car getCar() {
        return this.car;
    }
}

Main


    public class Main {
    /*
    We will create two engineers, which will implement the Worker interface.
    They will be able to build a car using the CarBuilder Methods (both will use this class).
    Each Worker has a field of Car in his class. so he can build it while using CarPlan interface methods.
     */

    public static void main(String[] args)
    {
        MustangWorker engineer1 = new MustangWorker();
        MazdaWorker engineer2= new MazdaWorker();
        CarBuilder carBuilder1 = new CarBuilder(engineer1);
        CarBuilder carBuilder2 = new CarBuilder(engineer2);

        carBuilder1.buildACar();
        carBuilder2.buildACar();

        Car car1 = carBuilder1.getCar();
        Car car2 = carBuilder2.getCar();

        System.out.println("Two brands of cars were made by using the Builder design pattern:");
        System.out.println(car1.toString());
        System.out.println(car2.toString());
    }
}

Running Example

Two brands of cars were made by using the Builder design pattern:
A Car was made of the following parts: Engine: Mustang D-code, 5.0L V8 , Wheels: Silver AMR Wheels 2018, Gear: Manual , Car color: Red
A Car was made of the following parts: Engine: Mazda Engn 3.5L, V6 , Wheels: Black TBS Wheels 1997, Gear: Automatic , Car color: Green