Click here to Skip to main content
15,885,985 members
Articles / Builder
Article

Builder Pattern

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL4 min read 6.1K   1  
The builder pattern describe a way to separate an object from it's construction.The same construction method can create different representation of

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

The builder pattern describe a way to separate an object from it's construction.
The same construction method can create different representation of the object.
Use the pattern in the following situations:
  • You need to construct a complex object which has different
    representation.
  • The algorithm for creating the object should be independent of
    the way the object's parts is assembled.

For a UML diagram of the pattern go to dofactory site.

C# Example
Lets look at a builder example:

    #region Director

 

    /// <summary>

    /// The contractor is responsible for the

    /// house build.

    /// </summary>

    class Contractor

    {

        #region Methods

 

        /// <summary>

        /// Construct a new house with the given

        /// house builder

        /// </summary>

        /// <param name="house"></param>

        public void Construct(HouseBuilder houseBuilder)

        {

            houseBuilder.BuildFloor();

            houseBuilder.BuildWalls();

            houseBuilder.BuildDoors();

            houseBuilder.BuildWindows();

        }

 

        #endregion

    }

 

    #endregion

 

    #region Builder

 

    abstract class HouseBuilder

    {

        #region Members

 

        protected House _house;

 

        #endregion

 

        #region Properties

 

        public House House

        {

            get

            {

                return _house;

            }

        }

 

        #endregion

 

        #region Build Methods

 

        public abstract void BuildDoors();

        public abstract void BuildWindows();

        public abstract void BuildWalls();

        public abstract void BuildFloor();

 

        #endregion

    }

 

    #endregion

 

    #region Builder Product

 

    class HousePart

    {

        #region Members

 

        private string _strPartName;

 

        #endregion

 

        #region Properties

 

        /// <summary>

        /// The part name

        /// </summary>

        public string PartName

        {

            get

            {

                return _strPartName;

            }

        }

 

        #endregion

 

        #region Ctor

 

        /// <summary>

        /// Construct a new house part with the given

        /// name.

        /// </summary>

        /// <param name="name">The given name</param>

        public HousePart(string name)

        {

            _strPartName = name;

        }

 

        #endregion

    }

 

    class House

    {

        #region Members

 

        private string _material;

        private List<HousePart> _houseParts;

 

        #endregion

 

        #region Property

 

        /// <summary>

        /// The building material

        /// </summary>

        public string Material

        {

            get

            {

                return _material;

            }

        }

 

        #endregion

 

        #region Ctor

 

        /// <summary>

        /// Construct a new house with the given

        /// material

        /// </summary>

        /// <param name="material">The given material</param>

        public House(string material)

        {

            _material = material;

            _houseParts = new List<HousePart>();

        }

 

        #endregion

 

        #region Methods

 

        /// <summary>

        /// Adds a part of the house to the house

        /// </summary>

        /// <param name="part">The part to add</param>

        public void AddPart(HousePart part)

        {

            _houseParts.Add(part);

        }

 

        #endregion

    }

 

    #endregion

 

    #region Builder Concretes

 

    class GlassHouseBuilder : HouseBuilder

    {

        #region Methods

 

        public override void BuildFloor()

        {

            _house = new House("glass");

            House.AddPart(new HousePart("stone floor"));

        }

 

        public override void BuildWalls()

        {

            House.AddPart(new HousePart(House.Material +

                " walls"));

        }

 

        public override void BuildDoors()

        {

            House.AddPart(new HousePart("wood doors"));

        }

 

        public override void BuildWindows()

        {

            House.AddPart(new HousePart(House.Material +

                " windows"));

        }

 

        #endregion

    }

 

    class WoodHouseBuilder : HouseBuilder

    {

        #region Methods

 

        public override void BuildFloor()

        {

            _house = new House("wood");

            House.AddPart(new HousePart(House.Material +

                " floor"));

        }

 

        public override void BuildWalls()

        {

            House.AddPart(new HousePart(House.Material +

                " floor"));

        }

 

        public override void BuildDoors()

        {

            House.AddPart(new HousePart("iron doors"));

        }

 

        public override void BuildWindows()

        {

            House.AddPart(new HousePart("glass floor"));

        }

 

        #endregion

    }

 

    #endregion

The example is easy to understand. When have a house contractor that is
responsible to build the house in the right order. We have a builder
abstract class that defines the interface for the concrete builders.
Each concrete responsible to implement the abstract methods and
therefore build the parts differently.

Summary
To sum up, the builder pattern isn't commonly used.
It resemble the abstract factory in some ways but the difference between
the two patterns is that builder is concerned with how the object is built by
different concretes and abstract factory is concerned about what products
are built. As you could see in the example there is a director that organize
the building parts therefore making the algorithm of construction abstract.

This article was originally posted at http://wiki.asp.net/page.aspx/498/builder-pattern

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --