Thursday, September 11, 2014

Builder Design Pattern


Definition 

Builder Design pattern is a creational design pattern that is used to create a complex object that can have different representations. It segregates the construction process of the object and provides different values to the attributes for different objects that belongs to the same class.

Intention
  1. Construction Process is separated by underlying Product to be created. 
  2. The steps of construction process needs to be changed without touching the underlying Product.
  3. Different type of similar product has to be added without modifying the construction process. 

Components

Product - The complex object that needs to be created.
Abstract Builder - An interface or abstract class that provides common methods to create and assemble the Product.
Concrete Builder - Specific implementation of Abstract Builder that creates and assembles different Products.
Director - Class constructs the complex object using the Builder interface used by client.
Client - Who needs an instance of a complex Product and operates on it.



Diagram









As you can see in the above diagram Vehicle Director class has the construct()  method which comprise the steps required to create Product. However, the assemble process is consulted to the specific concrete Builder. These two tasks are insulated by the Abstract Builder.


Code:

Example used here is extension to Factory Method example.

Abstract Builder

 

Concrete Builders





Products



public class Bike implements Vehicle {

    static {
        VehicleFactory.instance().registerProduct("Bike", new Bike());
    }

    private int numberOfTyres;
    private String name;

    @Override
    public String toString() {
        return "Bike [numberOfTyres=" + numberOfTyres + ", name=" + name + "]";
    }

    @Override
    public Vehicle createVehicle() {
        return new Bike();

    }

    @Override
    public void getEngine() {

        System.out.println("This is a Bike Engine");

    }

    @Override
    public void setNumberOfTyres(int numberOfTyres) {
        this.numberOfTyres = numberOfTyres;

    }

    @Override
    public void setName(String name) {
        this.name = name;

    }

    @Override
    public int getNumberOfTyres() {

        return this.numberOfTyres;
    }

    @Override
    public String getName() {

        return this.name;
    }

}


public class Car implements Vehicle {

    @Override
    public String toString() {
        return "Car [numberOfTyres=" + numberOfTyres + ", name=" + name + "]";
    }

    static {
        VehicleFactory.instance().registerProduct("Car", new Car());
    }

    private int numberOfTyres;
    private String name;

    @Override
    public void getEngine() {

        System.out.println("This is a Car Engine");
    }

    @Override
    public Vehicle createVehicle() {
        return new Car();

    }

    @Override
    public void setNumberOfTyres(int numberOfTyres) {
        this.numberOfTyres = numberOfTyres;

    }

    @Override
    public void setName(String name) {
        this.name = name;

    }

    @Override
    public int getNumberOfTyres() {

        return this.numberOfTyres;
    }

    @Override
    public String getName() {

        return this.name;
    }

}



Director




Client


You can add new Product only creating a new Builder(If Product is interface as the example you can create  an anonymous inner class) and register it with the client.

If you want to remove the steps from the construction process, you only have to do changing the Director class.




No comments: