paint-brush
How to Use the Builder Pattern in C# by@ssukhpinder
453 reads
453 reads

How to Use the Builder Pattern in C#

by Sukhpinder SinghJune 7th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The article demonstrates the usage of builder design patterns using the C# programming language. According to Gang of Four, a creational pattern “Builder’s” allows to separate and reuse a specific method to build something. The article shows how gracefully we can build different car models using the builder design pattern. The code pattern is highly maintainable & extensible. If in the future if we need to develop a new model, just the new model needs to extend the CarBuilder class, and it's done.

Company Mentioned

Mention Thumbnail
featured image - How to Use the Builder Pattern in C#
Sukhpinder Singh HackerNoon profile picture
According to Gang of Four, a creational pattern “Builder” allows to separate and reuse a specific method to build something.

Use Case

Lets us take an example of a Car, and the user wanted to build two models, i.e., SUV and Sedan. Builder design pattern comes in handy in the above use case, and let's see a step-by-step demonstration.And let's assume the Car class has the following properties.
public class Car{
 public string Name { get; set; }
 public double TopSpeed { get; set; }
 public bool IsSUV { get; set; }
}

Prerequisites

Basic knowledge of OOPS concepts.Any programming language knowledge.

The article demonstrates the usage of builder design patterns using the C# programming language. So, to begin with,

Getting Started

Firstly, let’s implement an abstract class builder extended by different car models like SUV or Sedan as per use case.The abstract class consists of the following methods
public abstract class CarBuilder
{
    protected readonly Car _car = new Car();
    public abstract void SetName();
    public abstract void SetSpeed();
    public abstract void SetIsSUV();
    public virtual Car GetCar() => _car;
}
Abstract methods for each property of the Car class. A virtual method that outputs the Car class instance.Now, let’s create a factory that utilizes CarBuilder class to build different car models and returns the instance of the car made.
public class CarFactory
{
    public Car Build(CarBuilder builder)
    {
        builder.SetName();
        builder.SetSpeed();
        builder.SetIsSUV();
        return builder.GetCar();
    }
}
Finally, implement different models of cars. Firstly, ModelSuv.cs
public class ModelSuv : CarBuilder
{
    public override void SetIsSUV()
    {
        _car.IsSUV = true;
    }

    public override void SetName()
    {
        _car.Name = "Maruti SUV";
    }

    public override void SetSpeed()
    {
        _car.TopSpeed = 1000;
    }
}
Lastly, ModelSedan.cs
public class ModelSedan : CarBuilder
{
    public override void SetIsSUV()
    {
        _car.IsSUV = false;
    }

    public override void SetName()
    {
        _car.Name = "Maruti Sedan";
    }

    public override void SetSpeed()
    {
        _car.TopSpeed = 2000;
    }
}

How to use builder pattern from the Main() method

Finally, let's use design patterns to build different car models with the help of 

factory.Build(<model>) 
method.

static void Main(string[] args)
{
    var sedan = new ModelSedan();
    var suv = new ModelSuv();

    var factory = new CarFactory();
    var builders = new List<CarBuilder> { suv, sedan };

    foreach (var b in builders)
    {
        var c = factory.Build(b);
        Console.WriteLine($"The Car details" +
            $"\n--------------------------------------" +
            $"\nName: {c.Name}" +
            $"\nIs SUV: {c.IsSUV}" +
            $"\nTop Speed: {c.TopSpeed} mph\n");
    }
}
The above usage shows how gracefully we can build different car models using the builder design pattern.The code pattern is highly maintainable & extensible. If in the future if we need to develop a new model, just the new model needs to extend the CarBuilder class, and it's done.

Output

Github Repo

The following repository shows the above use case implementation using a Builder Design Pattern in the console-based application.

Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.

Also published on:

바카라사이트 바카라사이트 온라인바카라