Please enable Javascript to correctly display the contents on Dot Net Tricks!

Builder Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 04 Jun 2013
Total Views : 57,840   
Updated On : 26 Sep 2016
 

Builder pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is used to builds a complex object by using a step by step approach. It provides an interface for creating parts of a product. In this article, I would like share what is builder pattern and how is it work?

What is Builder Pattern?

Builder pattern builds a complex object by using a step by step approach. Builder interface defines the steps to build the final object. This builder is independent from the objects creation process. A class that is known as Director, controls the object creation process.

Moreover, builder pattern describes a way to separate an object from its construction. The same construction method can create different representation of the object.

Builder Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the builder design pattern is given below:

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. Builder

    This is an interface which is used to define all the steps to create a product

  2. ConcreteBuilder

    This is a class which implements the Builder interface to create complex product.

  3. Product

    This is a class which defines the parts of the complex object which are to be generated by the builder pattern.

  4. Director

    This is a class which is used to construct an object using the Builder interface.

C# - Implementation Code

public interface IBuilder
{
 void BuildPart1();
 void BuildPart2();
 void BuildPart3();
 Product GetProduct();
}
 
public class ConcreteBuilder : IBuilder
{
 private Product _product = new Product();
 
 public void BuildPart1()
 {
 _product.Part1 = "Part 1";
 }
 
 public void BuildPart2()
 {
 _product.Part2 = "Part 2";
 }
 
 public void BuildPart3()
 {
 _product.Part3 = "Part 3";
 }
 
 public Product GetProduct()
 {
 return _product;
 }
}
 
public class Product
{
 public string Part1 { get; set; }
 public string Part2 { get; set; }
 public string Part3 { get; set; }
}

public class Director
{
 public void Construct(IBuilder IBuilder)
 {
 IBuilder.BuildPart1();
 IBuilder.BuildPart2();
 IBuilder.BuildPart3();
 }
}

Builder Pattern - Example

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:

  1. IVehicleBuilder - Builder interface

  2. HeroBuilder & HondaBuilder- Concrete Builder

  3. Vehicle- Product

  4. Vehicle Creator - Director

C# - Sample Code

/// <summary>
/// The 'Builder' interface
/// </summary>
public interface IVehicleBuilder
{
 void SetModel();
 void SetEngine();
 void SetTransmission();
 void SetBody();
 void SetAccessories();

 Vehicle GetVehicle();
}

/// <summary>
/// The 'ConcreteBuilder1' class
/// </summary>
public class HeroBuilder : IVehicleBuilder
{
 Vehicle objVehicle = new Vehicle();
 public void SetModel()
 {
 objVehicle.Model = "Hero";
 }

 public void SetEngine()
 {
 objVehicle.Engine = "4 Stroke";
 }

 public void SetTransmission()
 {
 objVehicle.Transmission = "120 km/hr";
 }

 public void SetBody()
 {
 objVehicle.Body = "Plastic";
 }

 public void SetAccessories()
 {
 objVehicle.Accessories.Add("Seat Cover");
 objVehicle.Accessories.Add("Rear Mirror");
 }

 public Vehicle GetVehicle()
 {
 return objVehicle;
 }
}

/// <summary>
/// The 'ConcreteBuilder2' class
/// </summary>
public class HondaBuilder : IVehicleBuilder
{
 Vehicle objVehicle = new Vehicle();
 public void SetModel()
 {
 objVehicle.Model = "Honda";
 }

 public void SetEngine()
 {
 objVehicle.Engine = "4 Stroke";
 }

 public void SetTransmission()
 {
 objVehicle.Transmission = "125 Km/hr";
 }

 public void SetBody()
 {
 objVehicle.Body = "Plastic";
 }

 public void SetAccessories()
 {
 objVehicle.Accessories.Add("Seat Cover");
 objVehicle.Accessories.Add("Rear Mirror");
 objVehicle.Accessories.Add("Helmet");
 }

 public Vehicle GetVehicle()
 {
 return objVehicle;
 }
}

/// <summary>
/// The 'Product' class
/// </summary>
public class Vehicle
{
 public string Model { get; set; }
 public string Engine { get; set; }
 public string Transmission { get; set; }
 public string Body { get; set; }
 public List<string> Accessories { get; set; }

 public Vehicle()
 {
 Accessories = new List<string>();
 }

 public void ShowInfo()
 {
 Console.WriteLine("Model: {0}", Model);
 Console.WriteLine("Engine: {0}", Engine);
 Console.WriteLine("Body: {0}", Body);
 Console.WriteLine("Transmission: {0}", Transmission);
 Console.WriteLine("Accessories:");
 foreach (var accessory in Accessories)
 {
 Console.WriteLine("\t{0}", accessory);
 }
 }
}

/// <summary>
/// The 'Director' class
/// </summary>
public class VehicleCreator
{
 private readonly IVehicleBuilder objBuilder;

 public VehicleCreator(IVehicleBuilder builder)
 {
 objBuilder = builder;
 }

 public void CreateVehicle()
 {
 objBuilder.SetModel();
 objBuilder.SetEngine();
 objBuilder.SetBody();
 objBuilder.SetTransmission();
 objBuilder.SetAccessories();
 }

 public Vehicle GetVehicle()
 {
 return objBuilder.GetVehicle();
 }
}

/// <summary>
/// Builder Design Pattern Demo
/// </summary>
class Program
{
 static void Main(string[] args)
 {
 var vehicleCreator = new VehicleCreator(new HeroBuilder());
 vehicleCreator.CreateVehicle();
 var vehicle = vehicleCreator.GetVehicle();
 vehicle.ShowInfo();

 Console.WriteLine("---------------------------------------------");

 vehicleCreator = new VehicleCreator(new HondaBuilder());
 vehicleCreator.CreateVehicle();
 vehicle = vehicleCreator.GetVehicle();
 vehicle.ShowInfo();

 Console.ReadKey();
 }
}

Builder Pattern Demo - Output

When to use it?

  1. Need to create an object in several steps (a step by step approach).

  2. The creation of objects should be independent from the way the object's parts are assembled.

  3. Runtime control over the creation process is required.

What do you think?

I hope you will enjoy the Builder Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
3 AUG
ASP.NET Core with Angular4 Development (Online)

07:00 AM - 9:00 AM IST(+5:30 GMT)

Know More
29 JUL
Angular2 and Angular4 Development (Online)

08:00 AM - 10:00 AM IST(+5.30 GMT)

Know More
29 JUL
ASP.NET MVC with Angular4 Development (Classroom)

09:30 AM - 11:00 AM IST (+5:30GMT)

Know More
29 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

09:00 AM - 11:00 AM IST(+5:30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

Know More
22 JUL
MEAN Stack 2 Development (Classroom)

04:00 PM - 05:30 PM IST(+5:30GMT)

20 JUL
Hadoop Development cum Administration (Online)

07:00 AM - 09:00 AM IST(+5:30 GMT)

17 JUL
NodeJS with Angular4 Development (Online)

08:00 PM - 09:30 PM IST(+5.30 GMT)

15 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM - 05:30 PM IST(+5.30 GMT)

1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

08:00 AM - 09:30 AM IST

1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM - 12:30 PM IST(+5:30 GMT)

30 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

09:30 PM - 11:00PM IST(+5:30GMT)

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
NodeJS with Angular4 Development (Classroom)

05:00 PM - 07:00 PM

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+