Builder Design Pattern in C# with Real-time Example in C#
In this article, I will talk about the Builder Design Pattern in C# with models. The Builder Design Pattern is one of the most often utilized plan designs progressively applications. The Builder Design Pattern in C# falls under the classification of 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 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
Implementation of Builder Design Pattern Real-time Example in C#
As per the real time example for Builder design pattern is we are going build a laptop for a employee, and for doing a setup or build a laptop , we need couple of the configuration and those are Builder RAM, SSD, USB Mouse etc
So lets build a laptop
Step 1: Create a class file with the name SystemDetails.cs and then copy and paste the following code in it. This is going to be our product and in this class, we put all the attributes like SSD, RAM, USB, 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 in 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 Systemtypes such as Laptop, Desktop, 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();
}
}
|
Step5: Client Code.
Now lets write below code in 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 instance, then we call the MakeSystem method on the SystemDirector instance bypassing the LaptopBuilder instance as an argument that will create and return the lptop. Again you need to follow the same process to make 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