Design Pattern Tutorials

Builder Design Pattern Introduction

In this tutorial we will learn :

  • What is Builder Design Pattern
  • Implementation Guidelines
  • Builder design pattern in action

What is Builder Design Pattern : Builder Pattern belongs to creational pattern

Definition : "Separate the construction of a complex object from its representation so that the same construction process can create different representations"
Builder Pattern solves the situation of increasing constructor parameters and constructors of a given class by providing a step by step initialisation of Parameters. After step by step initialisation, it returns the resulting constructed Object at once.
Implementation Guidelines : We need to Choose Builder Design Pattern when
  • We need to break up the construction of a complex object
  • We need to create a complex object and it should be independent of the parts that make up the object
  • The construction process must allow multiple representations of the same class

Business Requirement : Provide an option to choose and build configuration of the system which is allocated to the employees. The configuration options that user can choose are RAM, HDD, USB Mouse etc.

If you look at the illustrated diagram of Builder Design Pattern

Builder : Defines a template for the steps to construct the product. To simplify, Builder Specifies an abstract interface for creating parts of a Product object.
Concrete Builder : Implements the builder interface and provides an interface for getting the product.
Director : Is responsible to constructs the object through the builder interface.
Product : is the Main object that's constructed and Represents the complex object.

Steps to solve the above business requirement


Step 1 : Add SystemConfigurationDetails to the existing Employee table

CREATE TABLE [dbo].[Employee]
(
    [Id]               INT          IDENTITY (1, 1) NOT NULL,
    [Name]             VARCHAR (50) NOT NULL,
    [JobDescription]   VARCHAR (50) NOT NULL,
    [Number]           VARCHAR (50) NOT NULL,
    [Department]       VARCHAR (50) NOT NULL,
    [HourlyPay]        DECIMAL (18) NOT NULL,
    [Bonus]            DECIMAL (18) NOT NULL,
    [EmployeeTypeID]   INT          NOT NULL,
    [HouseAllowance]   DECIMAL (18) NULL,
    [MedicalAllowance] DECIMAL (18) NULL,
    [ComputerDetails]  VARCHAR (250) NULL,
    [SystemConfigruationDetails]  VARCHAR (500) NULL,

    PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Employee_EmployeeType]
     FOREIGN KEY ([EmployeeTypeID])
     REFERENCES [dbo].[Employee_Type] ([Id])
)

Step 2 : Open EmployeePortal.edmx under the Models folder of the solution and update the model from the database (Right click on the model designer and choose update from database option)
Step 3 : Create BuildSystem Action method and generate a view out of it.
[HttpGet]
public ActionResult BuildSystem(int? employeeID)
{

return View(employeeID);
}

Step 4 : Create a new class and name it as computer system with RAM and HDDSize as the constructor parameters and Build as method that builds the system.
public class ComputerSystem
{
        private string _RAM;
        private string _HDDSize;

        public ComputerSystem()
        {
        }

        public ComputerSystem(string RAM, string HDD)
        {
            _RAM = RAM;
            _HDDSize = HDD;
        }

        public string Build()
        {
            StringBuilder sb = new StringBuilder();
          
            sb.Append(string.Format(" RAM: {0}", _RAM));
      
            sb.Append(string.Format(" HDDSize: {0}", _HDDSize));
           
            return sb.ToString();
        }
}


Step 5 : To the added BuildSystem view add dropdown lists to choose RAM and HDDSize.
Step 6 : Create an action method BuildSystem to build the system on Submit.

[HttpPost]
public ActionResult BuildSystem(int employeeID, string RAM, string HDDSize)
{
     Employee employee = db.Employees.Find(employeeID);
     ComputerSystem computerSystem = new ComputerSystem(RAM, HDDSize);
     employee.SystemConfigurationDetails = computerSystem.Build();
     db.Entry(employee).State = EntityState.Modified;
     db.SaveChanges();
     return RedirectToAction("Index");
}


Step 7 : Run the application and Build a system for selected employee

Step 8 : Choose the configurations and click on create to build the system

Though we have achieved the business requirement. The current approach will not fit if we need to add more configurations to the current system and it gets complicated.
Also, if we need to enhance the current code itself to segregate the configurations differently between a laptop and desktop the current approach is not feasible and it becomes complicated.
In these kind of situations builder pattern comes to our rescue.

;