What is it?
Builder is a creational design pattern that is used to construct a complex object from simple objects using a step-by-step approach
Real World Analogy:
If you have ever been on Tesla website you must have seen they have a step by step way of selecting a car that starts from a base model and user can keep on adding features as they want. This can be handled in code by telescoping a constructor and having all the possible combinations but what if a new feature is introduced.
Solution
The answer is Builder design pattern. The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate
objects called builders. Instead of telescoping a constructor to handle all the cases we build the car using features selected by a user.
Code Example:
public class Director {
BuilderInterface myBuilder;
public void construct(BuilderInterface builder) {
myBuilder = builder;
myBuilder.buildBody();
myBuilder.insertWheels();
myBuilder.addHeadlights();
}
}
public class Product {
private LinkedList<String> parts;
public Product () {
parts = new LinkedList<String>();
}
public void add(String part) {
parts.addLast(part);
}
public void show() {
System.out.println("\n Product completed as below");
for(int i = 0; i < parts.size(); i++)
System.out.println(parts.get(i));
}
}
public class Client {
public static void main(String [] args) {
System.out.println("***Builder Pattern Demo***\n");
Director director = new Director();
BuilderInterface carBuilder = new Car();
BuilderInterface motorBuilder = new MotorCycle();
// making a car
director.construct(carBuilder);
Product p1 = carBuilder.getVehicle();
p1.show();
// making a motorcycle
director.construct(motorBuilder);
Product p2 = motorBuilder.getVehicle();
p2.show();
}
}
interface BuilderInterface {
void buildBody();
void insertWheels();
void addHeadlights();
Product getVehicle();
}
class Car implements BuilderInterface {
private Product product = new Product();
@Override
public void buildBody() {
product.add("This is a body of a car");
}
@Override
public void insertWheels() {
product.add("4 wheels are added");
}
@Override
public void addHeadlights() {
product.add("2 headlights are added");
}
@Override
public Product getVehicle() {
return product;
}
}
class MotorCycle implements BuilderInterface
{
private Product product = new Product();
@Override
public void buildBody() {
product.add("This is a body of a Motorcycle");
}
@Override
public void insertWheels() {
product.add("2 wheels are added");
}
@Override
public void addHeadlights() {
product.add("1 Headlight has been added");
}
@Override
public Product getVehicle() {
return product;
}
}
Advantage:
- It provides clear separation between the construction and representation of an object.
- It provides better control over construction process.
- It supports to change the internal representation of objects.
- Single responsibility principle.
DisAdvantages
- Complexity of code increases.
Github Link:
Builder Design Pattern Github repo
Top comments (1)
Excellent post you have shared with us . i appreciate your work that you take time to shared this . Best packers and movers in gurgaon