Builder Pattern

Introduction and implementation of builder design pattern


Category: Design Patterns Tags: C#

Builder Pattern - Code Files

Introduction

    Builder design pattern is a creational design pattern which is used when object creation is a step by step process. To reuse and simply object creation process we can create a builder class which can have methods to initialize different modules/properties of the object.

Suppose you want to build a computer and there are multiple steps done to create computer object like installing motherboard, RAM, CPU etc. Every time initializing these properties whenever we need computer object is not feasible. Lets see in implementation how this can be simplified using a builder.

Implementation

    Lets create a class Computer with all required properties.

public class Computer
{
public string Motherboard { get; set; }
public string CPU { get; set; }
public string RAM { get; set; }
public int Fans { get; set; }
public string PowerSupply { get; set; }
public string Battery { get; set; }
}

Above is the object we want to be initialized whenever we need a computer object.

public interface IComputerBuilder
{
void InstallMotherboard(string motherboard);
void InstallRAM(string ram);
void InstallCPU(string cpu);
void InstallFans(int fans);
void InstallPowerSupply(string powerSupply);
void InstallBattery(string battery);
Computer GetComputer();
}

Implementation of IComputerBuilder is following.

public class ComputerBuilder : IComputerBuilder
{
private Computer _computer;

public ComputerBuilder()
{
_computer = new Computer();
}

public void InstallMotherboard(string motherboard)
{
_computer.Motherboard = motherboard;
}
public void InstallRAM(string ram)
{
_computer.RAM = ram;
}
public void InstallCPU(string cpu)
{
_computer.CPU = cpu;
}
public void InstallFans(int fans)
{
_computer.Fans = fans;
}
public void InstallPowerSupply(string powerSupply)
{
_computer.PowerSupply = powerSupply;
}
public void InstallBattery(string battery)
{
_computer.Battery = battery;
}

public Computer GetComputer()
{
var computer = _computer;
_computer = new Computer();
return computer;
}
}

In above implementation we can see GetComputer method returns an object of Computer class. We can use other methods to initialize other fields like CPU and RAM. Now we can directly use this builder to create computer objects but in most of the cases we will have to install all the components in a computer and a computer can a laptop or desktop which have fixed set of components. Lets simplify this more by adding a director class as following.

public class ComputerDirector
{
private IComputerBuilder _computerBuilder;
public ComputerDirector(IComputerBuilder computerBuilder)
{
_computerBuilder = computerBuilder;
}

public Computer GetLaptop()
{
_computerBuilder.InstallMotherboard("HP motherboard");
_computerBuilder.InstallRAM("8GB*2");
_computerBuilder.InstallCPU("Intel i5");
_computerBuilder.InstallFans(1);
_computerBuilder.InstallBattery("10000mah");
return _computerBuilder.GetComputer();
}

public Computer GetDesktop()
{
_computerBuilder.InstallMotherboard("ASUS motherboard");
_computerBuilder.InstallRAM("8GB*4");
_computerBuilder.InstallCPU("AMD ryzen 7600");
_computerBuilder.InstallFans(4);
_computerBuilder.InstallPowerSupply("750watts");
return _computerBuilder.GetComputer();
}
}

Now Computer object is created by just one method call. Laptop and desktop objects can be created based on predefined settings which makes object creation very easy.

IComputerBuilder computerBuilder = new ComputerBuilder();
ComputerDirector computerDirector = new ComputerDirector(computerBuilder);
var laptop = computerDirector.GetLaptop();
var desktop = computerDirector.GetDesktop();
Console.WriteLine($"Fans installed in Laptop: {laptop.Fans}");
Console.WriteLine($"Fans installed in Desktop: {desktop.Fans}");

Output

Fans installed in Laptop: 1
Fans installed in Desktop: 4

Advantages of a builder:

  1. No need to overload constructor of a class with a lot of parameters which becomes messy if there are more than 5 parameters.
  2. Object creation can be simplified by just calling a single or a few methods.
  3. Using director we can set some predefined setting for different types of entities. Like different for desktop and laptop in above example.

 


Like 0 People
Last modified on 9 October 2022
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 164
Questions: 16
Given Best Solutions: 16 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x