In this article, we
will discuss the 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.
The Builder Design
Pattern is a creational design pattern that allows you to create complex
objects step by step. It separates the construction of a complex object from
its representation, allowing the same construction process to create different
representations.
Here's a real-time
example in C#: constructing a House object with various parts (walls, roof,
etc.).
Step 1: Define the Product Class
public class House {
public string Basement { get; set; }
public string Structure { get; set; }
public string Roof { get; set; }
public string Interior { get; set; }
public override string ToString()
{
return $"House with Basement: {Basement}, Structure: {Structure}, Roof: {Roof}, Interior: {Interior}";
}
}
Step 2: Define the Builder Interface
public interface IHouseBuilder
{
void BuildBasement();
void BuildStructure();
void BuildRoof();
void BuildInterior();
House GetHouse();
}
Step 3: Implement
Concrete Builders
public class ConcreteHouseBuilder : IHouseBuilder
{
private House _house = new House();
public void BuildBasement()
{
_house.Basement = "Concrete Basement";
}
public void BuildStructure()
{
_house.Structure = "Concrete and Steel Structure";
}
public void BuildRoof()
{
_house.Roof = "Concrete Roof";
}
public void BuildInterior()
{
_house.Interior = "Painted Walls with Wooden Flooring";
}
public House GetHouse()
{
return _house;
}
}
public class WoodenHouseBuilder : IHouseBuilder
{
private House _house = new House();
public void BuildBasement()
{
_house.Basement = "Wooden Basement";
}
public void BuildStructure()
{
_house.Structure = "Wood and Iron Structure";
}
public void BuildRoof()
{
_house.Roof = "Wooden Roof";
}
public void BuildInterior()
{
_house.Interior = "Wooden Walls with Carpet Flooring";
}
public House GetHouse()
{
return _house;
}
}
Step 4: Define the Director Class
public class Director
{
private IHouseBuilder _houseBuilder;
public Director(IHouseBuilder houseBuilder)
{
_houseBuilder = houseBuilder;
}
public void ConstructHouse()
{
_houseBuilder.BuildBasement();
_houseBuilder.BuildStructure();
_houseBuilder.BuildRoof();
_houseBuilder.BuildInterior();
}
public House GetHouse()
{
return _houseBuilder.GetHouse();
}
}
Step 5: Use the Builder Pattern
class Program
{
static void Main(string[] args)
{
IHouseBuilder concreteHouseBuilder = new ConcreteHouseBuilder();
Director director = new Director(concreteHouseBuilder);
director.ConstructHouse();
House concreteHouse = director.GetHouse();
Console.WriteLine(concreteHouse);
IHouseBuilder woodenHouseBuilder = new WoodenHouseBuilder();
director = new Director(woodenHouseBuilder);
director.ConstructHouse();
House woodenHouse = director.GetHouse();
Console.WriteLine(woodenHouse);
}
}
Explanation
- House: The product class, which has various parts such as Basement, Structure,
Roof, and Interior.
- IHouseBuilder: The builder interface with methods to
build different parts of the house and a method to get the final house
object.
- ConcreteHouseBuilder and WoodenHouseBuilder: Concrete
builder classes that implement the IHouseBuilder interface to build a
concrete house and a wooden house, respectively.
- Director: The director class that constructs a house using the builder
interface.
- Program: The client code that uses the builder pattern to create different
types of houses.
This example shows how
the Builder Design Pattern can be used to construct complex objects with a
step-by-step approach, ensuring the same construction process can produce
different representations of the object.
Share this
EmoticonEmoticon