In this article, we will discuss Builder Design Pattern in C# with models. it is part of the Creational Design Pattern.
Definition: "Separate the construction of a complex object from its representation so that the same construction process can create different representations"
Builder Design Pattern solves the situation of increasing constructor parameters and constructors of a given class by providing a step-by-step initialization of Parameters. After step-by-step initialization, 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
As per the real-time example for the Builder design pattern, we are going build a laptop for an employee, and for doing a setup or building a laptop, we need a couple of the configurations and those are Builder RAM, SSD, USB Mouse, etc
So let's build a laptop
Step 1: Create a class file with the name SystemDetails.cs and then copy and paste the following code into it. This is going to be our product and in this class, we put all the attributes like SSD, RAM, USB, and ProductName which are common to prepare a system(i.e. laptop or desktop). We also create one method (i.e. ShowProductInfo() ) which returns the System details.
public class SystemDetails
{
public int RAM
{ get; set; }
public int SSD
{ get; set; }
public int HDD
{ get; set; }
public string USB
{ get; set; }
public string
ProductName { get; set; }
public string
ShowProductInfo()
{
return "SystemName: " + ProductName + "
Contain these features " + RAM + " GB RAM,
" + SSD + "GB SSD, " + HDD
+ " GB HDD, " + USB + "
USB " + ProductName + "]\n";
}
}
|
Step2: Creating Abstract Builder (SystemBuilder)
Create a class file with the name SystemBuilder.cs and then copy and paste the following into it. This SystemBuilder class is going to be an abstract class and will act as a blueprint for any subclasses that want to create a System. So, it will have different subclass implementations for different system types such as laptops, desktops, etc. This class is having different abstract methods to Assign the RAM, HDD, SSD, USB, and SystemType.
public abstract class SystemBuilder
{
protected
SystemDetails details;
public void
CreateBeverage()
{
details = new
SystemDetails();
}
public
SystemDetails GetBeverage()
{
return
details;
}
public abstract void
SetSystemType();
public abstract void
AssignRam();
public abstract void
AssignSSD();
public abstract void
AssignHDD();
public abstract void
AssignUSB();
}
|
Step3: Creating Concrete Builder (LaptopBuilder and DesktopBuilder)
LaptopBuilder.cs
public class LaptopBuilder : SystemBuilder
{
public override void
SetSystemType()
{
Console.WriteLine("Step 1 : Assign System
Type");
GetSystem().ProductName = "Laptop";
}
public override void
AssignRam()
{
Console.WriteLine("Step 2 : Assign RAM");
GetSystem().RAM = 32;
}
public override void
AssignHDD()
{
Console.WriteLine("Step 3 : Assign HDD");
GetSystem().HDD = 30;
}
public override void
AssignSSD()
{
Console.WriteLine("Step 4 : Assign SSD");
GetSystem().SSD = 8;
}
public override void
AssignUSB()
{
Console.WriteLine("Step 5 : Assign USB");
GetSystem().USB = "USB";
}
}
|
DesktopBuilder.cs
public class DesktopBuilder :
SystemBuilder
{
public override void
SetSystemType()
{
Console.WriteLine("Step 1 : Assign System
Type");
GetSystem().ProductName = "Desktop";
}
public override void
AssignRam()
{
Console.WriteLine("Step 2 : Assign RAM");
GetSystem().RAM = 32;
}
public override void
AssignHDD()
{
Console.WriteLine("Step 3 : Assign HDD");
GetSystem().HDD = 30;
}
public override void
AssignSSD()
{
Console.WriteLine("Step 4 : Assign SSD");
GetSystem().SSD = 8;
}
public override void
AssignUSB()
{
Console.WriteLine("Step 5 : Assign USB");
GetSystem().USB = "USB";
}
}
|
Step4: Creating the Director (SystemDirector)
Once you created the concrete builder classes, then you need to need to create the director. The director is the component that will execute the required steps in a particular order to create a beverage.
So, create a class file with the name SystemDirector.cs and then copy and paste the following code in it. This class is having one generic method which will take SystemBuilder as an input parameter and then create and return a particular System object.
public class SystemDirector
{
public
SystemDetails MakeSystem(SystemBuilder builder)
{
builder.CreateSystem();
builder.SetSystemType();
builder.AssignRam();
builder.AssignHDD();
builder.AssignSSD();
builder.AssignUSB();
return
builder.GetSystem();
}
}
|
Step 5: Client Code.
Now let's write the below code in the main method. Here, first, we will create an instance of the SystemDirector class and then create an instance of the LaptopBuilder class. Once we have the SystemDirector and LaptopBuilder instances, then we call the MakeSystem method on the SystemDirector instance bypassing the LaptopBuilder instance as an argument that will create and return the laptop. Again you need to follow the same process to make a Desktop.
static void Main(string[] args)
{
SystemDetails
details;
SystemDirector
director = new SystemDirector();
LaptopBuilder
tea = new LaptopBuilder();
details =
director.MakeSystem(tea);
Console.WriteLine(details.ShowProductInfo());
DesktopBuilder
coffee = new DesktopBuilder();
details =
director.MakeSystem(coffee);
Console.WriteLine(details.ShowProductInfo());
Console.ReadLine();
}
|
Output
reference:-https://csharp-video-tutorials.blogspot.com/2017/09/builder-design-pattern-implementation.html