Builder Design Pattern in C#

In this article, I will explain how to implement builder design pattern in C#.

Builder design pattern comes under creational design pattern category of Gang of Four (GoF) design patterns. Builder design pattern is used to create complex type objects.

What is Builder Design Pattern?

Builder design pattern separates the complex object construction from its representation. It builds the complex object using some construction logic. This construction logic is implemented with step by step process. With different construction logic, different complex objects will be created.

Gang of Four (GoF) Definition:
“Separate the construction of a complex object from its representation so that the same construction process can create different representations.”

Builder Design Pattern – UML Diagram

Builder Design Pattern in C# - UML diagram

Participants in above UML diagram are –

  • Builder: Defines an interface for the steps to construct the product.
  • ConcreteBuilder: Defines a class that creates complex products, implements Builder interface.
  • Director: Defines a class that constructs the actual product object using builder interface.
  • Product: The main object that will be constructed.

Builder Design Pattern in C# – Example

To understand builder design pattern in C#, lets implement an example.

Problem Statement

Let’s say, there is a laptop manufacturing company which manufactures two types of laptop as follows:

  • Gaming Laptop
  • Normal Laptop

Now the accessories integrated in both the laptop are different, so different builders are required to manufacture these laptops.

In this example, I will create objects of gaming laptop and normal laptop using builder design pattern in C#.

Class Diagram

Builder design pattern in C# - class diagram

Components in above class diagram are:

  • ILaptopBuilder: Builder interface
  • GamingLaptopBuilder: Concrete Builder that implements Builder interface
  • NormalLaptopBuilder: Concrete Builder that implements Builder interface
  • LaptopManufacturer: Director class
  • Laptop: Product class

Builder Design Pattern in C# – Code Implementation

Result

Builder Design Pattern in C# - Result

Hope you get the idea about how to implement builder design pattern in C#. Please post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

One thought on “Builder Design Pattern in C#

  • July 18, 2016 at 10:40 am
    Permalink

    Good Article ..
    Sir …

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *