Builder Pattern

Definition

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Explanation

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. The telescoping constructor anti-pattern 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.

The builder pattern has another benefit. It can be used for objects that contain flat data (html code, SQL query, X.509 certificate…), that is to say, data that can’t be easily edited. This type of data cannot be edited step by step and must be edited at once. The best way to construct such an object is to use a builder class.

Builder often builds a Composite. Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed. Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components are built. Builders are good candidates for a fluent interface.

Screencast

TypeScript Code

module Builder {
    class Shop {
        private _vehicleBuilder: VehicleBuilder;

        public Construct(vehicleBuilder: VehicleBuilder): void {
            this._vehicleBuilder = vehicleBuilder;

            this._vehicleBuilder.BuildFrame();
            this._vehicleBuilder.BuildEngine();
            this._vehicleBuilder.BuildWheels();
            this._vehicleBuilder.BuildDoors();
        }

        public ShowVehicle(): void {
            this._vehicleBuilder.vehicle.display();
        }
    }

    class VehicleBuilder {
        private _vehicle: Vehicle = null;
        constructor(public vehicleType: VehicleType) {
            this._vehicle = new Vehicle(vehicleType);
        }

        public get vehicle(): Vehicle {
            return this._vehicle;
        }

        public BuildFrame(): void {
            throw new Error("Not implemented.");
        }

        public BuildEngine(): void {
            throw new Error("Not implemented.");
        }

        public BuildWheels(): void {
            throw new Error("Not implemented.");
        }

        public BuildDoors(): void {
            throw new Error("Not implemented.");
        }
    }

    class CarBuilder extends VehicleBuilder {
        constructor() {
            super(VehicleType.Car);
        }

        public BuildFrame(): void {
            this.vehicle.parts[PartType.Frame] = "Car Frame";
        }

        public BuildEngine(): void {
            this.vehicle.parts[PartType.Engine] = "2500 cc";
        }

        public BuildWheels(): void {
            this.vehicle.parts[PartType.Wheel] = "4";
        }

        public BuildDoors(): void {
            this.vehicle.parts[PartType.Door] = "4";
        }
    }

    class MotorCycleBuilder extends VehicleBuilder {
        constructor() {
            super(VehicleType.MotorCycle);
        }

        public BuildFrame(): void {
            this.vehicle.parts[PartType.Frame] = "MotorCycle Frame";
        }

        public BuildEngine(): void {
            this.vehicle.parts[PartType.Engine] = "500 cc";
        }

        public BuildWheels(): void {
            this.vehicle.parts[PartType.Wheel] = "2";
        }

        public BuildDoors(): void {
            this.vehicle.parts[PartType.Door] = "0";
        }
    }

    class ScooterBuilder extends VehicleBuilder {
        constructor() {
            super(VehicleType.Scooter);
        }

        public BuildFrame(): void {
            this.vehicle.parts[PartType.Frame] = "Scooter Frame";
        }

        public BuildEngine(): void {
            this.vehicle.parts[PartType.Engine] = "50 cc";
        }

        public BuildWheels(): void {
            this.vehicle.parts[PartType.Wheel] = "2";
        }

        public BuildDoors(): void {
            this.vehicle.parts[PartType.Door] = "0";
        }
    }

    class Vehicle {
        constructor(public vehicleType: VehicleType) {
            this.vehicleType = vehicleType;
        }

        private _parts: {} = {};
        public get parts(): {} {
            return this._parts;
        }

        public display() {
            Output.WriteLine("---------------------------");
            Output.WriteLine("Vehicle Type : " + VehicleType[this.vehicleType]);
            Output.WriteLine("Frame :" + this.parts[PartType.Frame]);
            Output.WriteLine("Engine :" + this.parts[PartType.Engine]);
            Output.WriteLine("#Wheels :" + this.parts[PartType.Wheel]);
            Output.WriteLine("#Doors :" + this.parts[PartType.Door]);
            Output.WriteLine("---------------------------");
        }
    }

    enum VehicleType {
        Car,
        Scooter,
        MotorCycle
    }

    enum PartType {
        Frame,
        Engine,
        Wheel,
        Door
    }

    window.addEventListener("load", function () {
        var shop = new Shop();

        shop.Construct(new ScooterBuilder());
        shop.ShowVehicle();

        shop.Construct(new CarBuilder());
        shop.ShowVehicle();

        shop.Construct(new MotorCycleBuilder());
        shop.ShowVehicle();
    });
}

Output