noscript

The Builder Design Pattern is a creational design pattern that provides a way to construct complex objects step-by-step. It separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

Key Components

1. Builder: An abstract interface for creating parts of a Product object.
2. Concrete Builder: A class that implements the Builder interface and provides specific implementations for the steps required to build the product.
3. Director: A class that constructs an object using the Builder interface. It controls the construction process.
4. Product: The complex object that is being built. It often has many parts.

Example of building a car using the Builder Design Pattern.

Product Class
public class Car {
private String engine;
private String wheels;
private String body;

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

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

public void setBody(String body) {
this.body = body;
}

@Override
public String toString() {
return “Car with engine: ” + engine + “, wheels: ” + wheels + “, body: ” + body;
}
}

Builder Interface
public interface CarBuilder {
void buildEngine();
void buildWheels();
void buildBody();
Car getCar();
}

ConcreteBuilder
public class SportsCarBuilder implements CarBuilder {
private Car car;

public SportsCarBuilder() {
this.car = new Car();
}

@Override
public void buildEngine() {
car.setEngine(“V8 Engine”);
}

@Override
public void buildWheels() {
car.setWheels(“Sport Wheels”);
}

@Override
public void buildBody() {
car.setBody(“Sport Body”);
}

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

Director
public class CarDirector {
private CarBuilder carBuilder;

public CarDirector(CarBuilder carBuilder) {
this.carBuilder = carBuilder;
}

public void buildCar() {
carBuilder.buildEngine();
carBuilder.buildWheels();
carBuilder.buildBody();
}

public Car getCar() {
return carBuilder.getCar();
}
}

Client Code
public class Client {
public static void main(String[] args) {
CarBuilder builder = new SportsCarBuilder();
CarDirector director = new CarDirector(builder);
director.buildCar();
Car car = director.getCar();
System.out.println(car);
}
}

Code Explanation :
1. Car: The `Product` class representing the complex object being built.
2. CarBuilder: The `Builder` interface declaring the steps to build a car.
3. SportsCarBuilder: The `ConcreteBuilder` class implementing the `CarBuilder` interface, providing specific steps to build a sports car.
4. CarDirector: The `Director` class that controls the construction process using a `CarBuilder`.
5. Client: The client code that puts everything together, using the `CarDirector` to build a car and retrieve it.

Benefits:

– Controlled Construction: The construction process is controlled and can be modified independently.
– Reusability: The same construction process can be used to create different representations.
– Flexibility: Builders can construct products step-by-step, allowing more control over the construction process.

When to Use:
– When you need to create a complex object with multiple parts.
– When you want to separate the construction process from the final representation.
– When you want to allow different representations of the same construction process.

                                                                   For more information & classes Call2048553004
                                                                                   Registration Link: Click Here!

Author: Ambarish Durani

JAVA Trainer

IT Education Centre Placement & Training Institute

© Copyright 2024 | IT Education Centre.