Jul 13, 2013

Builder Design Pattern

1 comment
Builder focuses on constructing a complex object step by step and final step will returns the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.
"Separate the construction of a complex object from its representation so that the same construction process can create different representations."

Example:
We can consider construction of a home. Home is the final end product (object) that is to be returned as the output of the construction process. It will have many steps, like basement construction, wall construction, roof construction and so on. Finally the whole home object is returned. Here using the same process you can build houses with different properties.

What is the difference between abstract factory and builder pattern? 
Abstract factory may also be used to construct a complex object. Then, what is the difference with builder pattern? Builder pattern emphasis is on ‘step by step’. Builder pattern will have number of small steps. Each step will have small units of logic enclosed in it. There will also be a sequence involved. It will start from step 1 and will go on up to step n and the final step is returning the object. In these steps, every step will add some value in construction of the object. That is you can imagine that the object grows stage by stage. Builder will return the object in last step. But in abstract factory how complex the built object might be, it will not have step by step object construction. 

Implementation of Builder Pattern
Following is the interface, which will be returned as the product from the builder.
using System;

namespace DesignPatterns.Builder
{
    public interface IHousePlan
    {
        void BuildBasement(string basement);
        void BuildWalls(string walls);
        void BuildRoof(string roof);
        void BuildInterior(string interior);
    }
}

Concrete class for the above interface. The builder constructs an implementation for the following class.
using System;

namespace DesignPatterns.Builder
{
    public class House : IHousePlan
    {
        public void BuildBasement(string basement)
        {
            Console.WriteLine("Basement constructed with " + basement);
        }

        public void BuildWalls(string walls)
        {
            Console.WriteLine("Walls constructed with " + walls);
        }

        public void BuildRoof(string roof)
        {
            Console.WriteLine("Roof constructed with " + roof);
        }

        public void BuildInterior(string interior)
        {
            Console.WriteLine("Interior constructed with " + interior);
        }
    }
}

Builder interface.
We will have multiple implementations of IHouseBuilder interface in order to facilitate, the same construction process to create different representations.
using System;

namespace DesignPatterns.Builder
{
    public interface IHouseBuilder
    {
        public void BuildBasement();
        public void BuildWalls();
        public void BulidRoof();
        public void BuildInterior();
        public House GetHouse();

    }
}

First implementation of a builder.
using System;

namespace DesignPatterns.Builder
{
    public class WoodenHouseBuilder : IHouseBuilder
    {
        private House _house;

        public WoodenHouseBuilder()
        {
            _house = new House();
        }

        public void BuildBasement()
        {
            _house.BuildBasement("Wooden Poles.");
        }

        public void BuildWalls()
        {
            _house.BuildWalls("Wood and Poles");
        }

        public void BulidRoof()
        {
            _house.BuildRoof("Wood and Seal skins");
        }

        public void BuildInterior()
        {
            _house.BuildInterior("Fire wood and Teak");
        }

        public House GetHouse()
        {
            return _house;
        }
    }
}

Second implementation of a builder. This is a type of Villa House.
using System;

namespace DesignPatterns.Builder
{
    public class VillaHouseBuilder : IHouseBuilder
    {
        private House _house;
        public VillaHouseBuilder()
        {
            _house = new House();
        }

        public void BuildBasement()
        {
            _house.BuildBasement("Concrete, Iron.");
        }

        public void BuildWalls()
        {
            _house.BuildWalls("Bricks");
        }

        public void BulidRoof()
        {
            _house.BuildRoof("Concrete, Iron");
        }

        public void BuildInterior()
        {
            _house.BuildInterior("Teak, Plywood etc.");
        }

        public House GetHouse()
        {
            return _house;
        }
    }
}

Following class constructs the house and most importantly, this maintains the building sequence of house.
using System;

namespace DesignPatterns.Builder
{
    public class CivilEngineer
    {
        private IHouseBuilder _housebuilder;

        public CivilEngineer(IHouseBuilder houseBuilder)
        {
            _housebuilder = houseBuilder;
        }

        public House GetHouse()
        {
            ConstructHouse();
            return _housebuilder.GetHouse();
        }

        private void ConstructHouse()
        {
            _housebuilder.BuildBasement();
            _housebuilder.BuildWalls();
            _housebuilder.BulidRoof();
            _housebuilder.BuildInterior();
        }
    }
}

Testing the sample builder design pattern implemented above,
private void btnBuilder_Click(object sender, EventArgs e)
{
    rtxtOutput.Clear();
    
    IHouseBuilder woodenHouseBuilder = new WoodenHouseBuilder();
    CivilEngineer engineer = new CivilEngineer(woodenHouseBuilder);
    House house = engineer.GetHouse();

    rtxtOutput.AppendText("Builder constructed house.");
}

Output of the above sample program:
       Basement constructed with Wooden Poles.
      Walls constructed with Wood and Poles
      Roof constructed with Wood and Seal skins
      Interior constructed with Fire wood and Teak
      Builder constructed house.

Builder Pattern in .Net BCL : DbConnectionStringBuilder
  - SqlConnectionStringBuilder
  - OleDbConnectionStringBuilder
  - OdbcConnectionStringBuilder

1 comment :