Builder Pattern - TechNet Articles - United States (English) - TechNet Wiki

This pattern creates object based on the Interface, but also lets the subclass decide which class to instantiate. It also has finer control over the construction process.

There is a concept of Director in Builder Pattern implementation. The director actually creates the object and also runs a few tasks after that.



public class="full-post">

Builder Pattern

This pattern creates object based on the Interface, but also lets the subclass decide which class to instantiate. It also has finer control over the construction process.

There is a concept of Director in Builder Pattern implementation. The director actually creates the object and also runs a few tasks aftinterface
IBuilder
    {
        string RunBulderTask1();
        string RunBuilderTask2();
    }
 
    public class Builder1 : IBuilder
    {
 
        #region IBuilder Members
 
        public string RunBulderTask1()
        {
            throw new ApplicationException("Task1");
        }
 
        public string RunBuilderTask2()
        {
            throw new ApplicationException("Task2");
        }
 
        #endregion
    }
 
    public class Builder2 : IBuilder
    {
        #region IBuilder Members
 
        public string RunBulderTask1()
        {
            return "Task3";
        }
 
        public string RunBuilderTask2()
        {
            return "Task4";
        }
 
        #endregion
    }
 
    public class Directore style="color:blue;">"Task4";
            {
        public IBuilder CreateBuilder(int type)
        {
            IBuilder builder = null;
            if (type == 1)
                builder = new Builder1();
            else
                builder = new Builder2();
            builder.RunBulderTask1();
            builder.RunBuilderTask2();
            return builder;
        }
    }

In case of Builder pattern you can see the Director is actually using CreateBuilder to create the instance of the builder. So when the Bulder is actually created, we can also invoke a few common task in it.