Builder Design Pattern Using C#

Builder design pattern falls under the category of "Creational" design patterns. This pattern is used to build a complex object by using a step by step approach.

GOF (Gang of Four) says:

“Separate the construction of a complex object from its representation so that the same construction process can create different representations”

So, it is used to construct a complex object step by step and the final step returns the object. Also, the process of constructing an object should be generic so that it can be used to create different representations of the same object.

Now, let’s see the UML diagram of it.

Builder Design Pattern Using C#

Below are the main parts of the UML diagram,

Builder

This is an interface which is used to define all the steps required to create a product.

ConcreteBuilder

This is a class which implements the Builder interface to create a complex product.

Product

This is a class which defines the parts of the complex object which are to be generated by the Builder Pattern.

Director

This is a class that is used to construct an object using the Builder interface.

So far, we have understood the UML diagram and the definition with its purpose. Now, it is time to look into the actual code to get a better understanding. So now, let’s see this with an example.

In the example shown below, we will be seeing how we can use this pattern to construct different parts of a toy.

This is our IToyBuilder interface which will be implemented by the ConcreteBuilder classes.

  1. public interface IToyBuilder {  
  2.     void SetModel();  
  3.     void SetHead();  
  4.     void SetLimbs();  
  5.     void SetBody();  
  6.     void SetLegs();  
  7.     Toy GetToy();  
  8. }  

This is the product class for Toy.

  1. public class Toy {  
  2.     public string Model {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Head {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Limbs {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Body {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string Legs {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }  

Below, we have 2 concrete classes for our two types of toys.

Toy A

  1. public class ToyABuilder: IToyBuilder {  
  2.     Toy toy = new Toy();  
  3.     public void SetModel() {  
  4.         toy.Model = "TOY A";  
  5.     }  
  6.     public void SetHead() {  
  7.         toy.Head = "1";  
  8.     }  
  9.     public void SetLimbs() {  
  10.         toy.Limbs = "4";  
  11.     }  
  12.     public void SetBody() {  
  13.         toy.Body = "Plastic";  
  14.     }  
  15.     public void SetLegs() {  
  16.         toy.Legs = "2";  
  17.     }  
  18.     public Toy GetToy() {  
  19.         return toy;  
  20.     }  

Toy B

  1. public class ToyBBuilder: IToyBuilder {  
  2.     Toy toy = new Toy();  
  3.     public void SetModel() {  
  4.         toy.Model = "TOY B";  
  5.     }  
  6.     public void SetHead() {  
  7.         toy.Head = "1";  
  8.     }  
  9.     public void SetLimbs() {  
  10.         toy.Limbs = "4";  
  11.     }  
  12.     public void SetBody() {  
  13.         toy.Body = "Steel";  
  14.     }  
  15.     public void SetLegs() {  
  16.         toy.Legs = "4";  
  17.     }  
  18.     public Toy GetToy() {  
  19.         return toy;  
  20.     }  
  21. }  

Let’s see the final director class which will be responsible for the construction of an object using the Builder Interface.

  1. public class ToyCreator {  
  2.     private IToyBuilder _toyBuilder;  
  3.     public ToyCreator(IToyBuilder toyBuilder) {  
  4.         _toyBuilder = toyBuilder;  
  5.     }  
  6.     public void CreateToy() {  
  7.         _toyBuilder.SetModel();  
  8.         _toyBuilder.SetHead();  
  9.         _toyBuilder.SetLimbs();  
  10.         _toyBuilder.SetBody();  
  11.         _toyBuilder.SetLegs();  
  12.     }  
  13.     public Toy GetToy() {  
  14.         return _toyBuilder.GetToy();  
  15.     }  
  16. }  

Finally, below is the main method of our program.

  1. void Main() {  
  2.     Console.WriteLine("-------------------------------List Of Toys--------------------------------------------");  
  3.     var toyACreator = new ToyCreator(new ToyABuilder());  
  4.     toyACreator.CreateToy();  
  5.     toyACreator.GetToy().Dump();  
  6.     var toyBCreator = new ToyCreator(new ToyBBuilder());  
  7.     toyBCreator.CreateToy();  
  8.     toyBCreator.GetToy().Dump();  
  9. }  

OUTPUT

Builder Design Pattern Using C#

Now, let’s understand when we should use this pattern with its advantages and disadvantages.

WHEN TO USE THIS PATTERN

We need to create a complex object in several steps (a step by step approach).

ADVANTAGES

  • Code is more maintainable and readable.
  • Less prone to errors as we have a method which returns the finally constructed object.

DISADVANTAGES

Number of lines of code increases in builder pattern, but it makes sense as the effort pays off in terms of maintainability and readability.
 

Conclusion

You should not use builder pattern just because it is a design pattern as no one should follow the best practice just because it is a best practice. 

So, if your object has only a few constructor arguments, it makes no sense to use the builder pattern.

Builder pattern is a good choice when designing classes whose constructors have more than a handful of parameters.

Consider builder pattern when faced with many constructor parameters, some of which are also optional. Using this code becomes much easier to read and write as compared to the traditional constructor pattern.

I hope you find this article helpful. Stay tuned for more … Cheers!!


Similar Articles