Thursday, July 13, 2023

The Builder design pattern using C#

The Builder design pattern using C#

The Builder pattern is a creational design pattern that lets you construct complex objects step by step. It separates the construction of an object from its representation so that the same construction process can create different representations. It's particularly useful when you need to create an object with lots of possible configuration options. The Builder design pattern is used when there is a need to construct complex objects step by step. It separates the construction of an object from its representation. By doing this, the same construction process can create different representations. Here, we'll use the Builder design pattern to construct a Monster object.
Let's break it down into different .cs files: we start with IMonster is the interface that outlines necessary steps for building a Monster object.
public interface IMonster
{
    void SetName(string name);
    void SetCatchPhase(string phrase);
    void SetHeadType(string type);
       
    Monster GetMonster();
}
The Monster class is the complex object that is being built.
public class Monster
{
    public string Name { get; set; }
    public string CatchPhrase { get; set; }
    public string HeadType { get; set; }
        
    public void Describe()
    {
        Console.WriteLine($"Monster name is {Name} he says {CatchPhrase} and his head type is {HeadType}");
    }
}
MonsterBuilder
public class MonsterBuilder : IMonster
{
    private Monster _monster = new Monster();
        
    public void SetName(string name)
    {
        _monster.Name = name;
    }

    public void SetCatchPhase(string phrase)
    {
        _monster.CatchPhrase = phrase;
    }

    public void SetHeadType(string type)
    {
        _monster.HeadType = type;
    }

    public Monster GetMonster()
    {
        return _monster;
    }
}
The DrFrankenstein class defines the order in which to execute the building steps.
public class DrFrankenstein
{
    public void Construct(IMonster monster)
    {
        monster.SetName("Frank");
        monster.SetCatchPhase("They did the monster mash");
        monster.SetHeadType("flat");
    }
}
We goto the Main in Program.cs
public static void Main(string[] args)
{
    DrFrankenstein drFrank = new DrFrankenstein();
    MonsterBuilder igor = new MonsterBuilder();
        
    // Director uses the builder to construct the object
    drFrank.Construct(igor);
        
    Monster monster = igor.GetMonster();
        
    monster.Describe();
}
When we comile and run we get
Monster name is Frank he says They did the monster mash and his head type is flat
You would use the Builder pattern when: The object creation process should be independent from the parts that make up the object. The construction process should allow different representations for the object that's constructed. The object has many potential configurations, or when increasing the number of constructor parameters would result in an explosion of constructors. In our case, a monster can have various types of bodies, different names, and power levels, and we want to avoid creating a new constructor for each combination. This is where the Builder pattern becomes useful.

No comments:

The Strategy Design Pattern a Behavioral Pattern using C++

The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm's implementation at runtime. Instead of i...