C# Builder Design Pattern

C# Builder Design Pattern

In this article you will learn how to implement builder design pattern. This is one of structural patterns. You can use it when you need to have control over how complex objects would be created at runtime. This pattern gives you step by step approach on the object creation process. This is useful if you want to reuse the same creation process to build different representations. I’ll demonstrate the implementation of the pattern on simplified  real word example. And I’ll explain it step by step.

To show you the pattern I created the system of creating two types of enemies, each process of creation takes multiple steps, that’s good opportunity for using builder.

UML diagram:

l1

 

  1. IEnemy – Builder interface
  2. EnemyTypeA and EnemyTypeB– Concrete Builder
  3. Enemy– Product
  4. EnemyCreator – Director

 

So first let’s create an enemy Class, we will instantiate objects of this class in Concrete Builders.

class Enemy
{
  public string model;
  public string behaviour;
  public string components;
}

Here we have three public variables just to  demonstrate the example.

Next we need Builder interface that will contains all the method that will be implemented by Enemies Classes. And we have Get Enemy method that will return the Enemy Class instance after competing the construction process.

interface IEnemy
{
  void SelectModel();
  void SelectBehaviour();
  void AddComponents();

  Enemy GetEnemy();
}

 

Next step is to create Classes based on the interface. Each class will have unique implementation of the interface methods.  In order to get access to Enemy Class properties we need to create instance of this Class.

class EnemyTypeA : IEnemy
{
  Enemy enemy = new Enemy();
  public void AddComponents()
  {
    enemy.components="Components Added";
  }

  public void SelectBehaviour()
  {
    enemy.behaviour= "Behaviour Type A Selected";
  }

  public void SelectModel()
  {
     enemy.model = "Body Type A Selected";
  }
  public Enemy GetEnemy()
  {
     return enemy;
  }
}
//EnemyTypeB
class EnemyTypeB : IEnemy
{
  Enemy enemy = new Enemy();
  public void AddComponents()
  {
     enemy.components = "Components Added";
  }

  public void SelectBehaviour()
  {
     enemy.behaviour = "Behaviour Type B Selected";
  }

  public void SelectModel()
  {
     enemy.model = "Body Type B Selected";
  }
  public Enemy GetEnemy()
  {
     return enemy;
  }
}

 

Now it’s time for creating Director that will contain the logic behind creating new enemy. In Class constructor we pass IEnemy interface, this way we can pass any Class that inhirates form this interface.

class EnemyCreator
{
  private readonly IEnemy enemyBuilder;

  public EnemyCreator( IEnemy enemyType)
  {
     enemyBuilder = enemyType;
  }
  public Enemy ConstructEnemy()
  {
    enemyBuilder.SelectBehaviour();
    enemyBuilder.SelectModel();
    enemyBuilder.AddComponents();

    return enemyBuilder.GetEnemy();
  }
}

Construct Enemy method return instance of Enemy Class with new properties.

Last thing to complete the implementation is to create Client Class that will  create instances of each enemy type.

class Program
{
  static void Main(string[] args)
  {
  var enemyCreator = new EnemyCreator(new EnemyTypeA());
  var enemy = enemyCreator.ConstructEnemy();
  enemy.ShowInfo();
  Console.WriteLine("_______________________");
  enemyCreator = new EnemyCreator(new EnemyTypeB());
  enemy = enemyCreator.ConstructEnemy();
  enemy.ShowInfo();
  Console.ReadLine();
  }
}

 

To conclude, Builder pattern Separates the construction of a complex object from its representation so that the same construction process can create different representations. I hope you found it useful and you will implement this pattern to you programs. I next article I’ll cover another exciting design patter. Stay Awesome!

Link To Full Source Code