Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Builder Pattern

Posted in Design Patterns
This post has been read 616 times

In continuation of my series on Creational design patterns, I will be discussing about the Builder pattern in this post. You can read about the other patterns from the following links.

  1. Prototype Pattern
  2. Factory Method Pattern
  3. Singleton Pattern
  4. Abstract Factory Pattern

You can read about Structural Patterns from here.

The Builder pattern deals with separation of the specification of a complex object from its actual construction. This construction process can create different representations of the class.

    1 public class BuilderPattern

    2 {

    3 

    4     // Interface Car

    5     public interface ICar

    6     {

    7         string Details { get; set; }

    8     }

    9 

   10     // All concrete Cars

   11     class Car : ICar

   12     {

   13         public string Details { get; set; }

   14     }

   15 

   16     // Directors

   17     private interface IModel

   18     {

   19         ICar CreateCar();

   20     }

   21 

   22     //Individual car model

   23     public class Benz : IModel

   24     {

   25         public ICar CreateCar()

   26         {

   27             var car = new Car();

   28             DoWork("Build chassis");

   29             DoWork("Fit Engine");

   30             DoWork("Paint");

   31             car.Details = "Merc";

   32             return car;

   33         }

   34     }

   35 

   36     //Individual car model

   37     public class BMW : IModel

   38     {

   39         public ICar CreateCar()

   40         {

   41             var car = new Car();

   42             DoWork( "Build chassis" );

   43             DoWork( "Fit Engine" );

   44             DoWork( "Paint" );

   45             car.Details = "BMW";

   46             return car;

   47         }

   48     }

   49 

   50     public static void DoWork( string workitem )

   51     {

   52         Console.Write("" + workitem + ": 0%");

   53         Console.Write("....25%");

   54         Console.Write("....50%");

   55         Console.WriteLine("....100%");

   56     }

   57 

   58     //Builder that creates the car

   59     private interface IBuilder<T> where T : IModel

   60     {

   61         ICar CreateCar();

   62     }

   63 

   64     // Concrete Builder

   65     class Builder<T> : IBuilder<T> where T : IModel, new()

   66     {

   67 

   68         T myModel;

   69         public Builder()

   70         {

   71             myModel = new T();

   72         }

   73 

   74         public ICar CreateCar()

   75         {

   76             return myModel.CreateCar();

   77         }

   78     }

   79 

   80     class Client<T> where T : IModel, new()

   81     {

   82         public void ClientMain()

   83         {

   84             IBuilder<T> builder = new Builder<T>();

   85             Console.WriteLine("Building a car");

   86             var car = builder.CreateCar();

   87 

   88             Console.WriteLine( "Car Built : " + car.Details );

   89         }

   90     }

   91 

   92     private static void Main()

   93     {

   94         new Client<Benz>().ClientMain();

   95         new Client<BMW>().ClientMain();

   96 

   97         // Output : Building a car

   98         //          Build chassis: 0%....25%....50%....100%

   99         //          Fit Engine: 0%....25%....50%....100%

  100         //          Paint: 0%....25%....50%....100%

  101         //          Car Built : Merc

  102         //

  103         //          Building a car

  104         //          Build chassis: 0%....25%....50%....100%

  105         //          Fit Engine: 0%....25%....50%....100%

  106         //          Paint: 0%....25%....50%....100%

  107         //          Car Built : BMW

  108 

  109         Console.ReadLine();

  110     }

  111 }

The Builder pattern is based on Directors and Builders. Builder classes can confirm to an IBuilder interface, and they can be called by a director to produce a product according to specification. Builders can be found in applications that create complex structures. We can use the builder pattern when The object to be assembled might have different representations. or when you need fine control over the construction process.

In my next post I will start with the Behavioral patterns.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll