Builder Design Pattern Using C# Sample

Introduction

 
Builder Design pattern is a creational design pattern. During construction of complex objects, the builder design pattern plays a key role.
 

Where to use Builder Design Pattern?

 
When we have to build complex objects; i.e., the main class itself contains multiple objects which need to be created to accomplish creating an object of main class, then we need to go for Builder design pattern. Here we need to define common object constructor to build all necessary objects.
 

Why use it?

 
During creation of multiple objects to accomplish the main class object, we come across one common function;  i.e., building object. In this case builder design pattern suggests defining a common class which is  only responsible for building the required object.
 
Players in this pattern are:
  • Builder – will define contract to build different parts of one product
  • ConcreteBuilder – will implement builder contract
  • Product – a complex product which contains several sub parts
  • ProductServer – the one who uses the product and provides its services to whomever requires it
We will see now how to implement it with an example.
 

Problem Definition

 
Build an application for payment card vendors who have to prepare magnetic strip and chip-based cards,  so that card venders can provide this to banks, and  banks can provide these services to customers.
 
For the above problem we will use builder design pattern, below are the players:
  • Builder – ICard
  • ConcreteBuilder – AbcPaymentCard, XyzPaymentCard
  • Product – Card
  • ProductServer – BankA
Below is the ICard Contract which will define what objects/parts need to be built:
  1. namespace Builder.Design.Pattern.Contract  
  2. {  
  3.     public interface ICard  
  4.     {  
  5.         void AddMagneticStrip();  
  6.         void AddChip();  
  7.         Card GetCard();  
  8.     }  
  9. }  
Below is AbcPaymentCard and XyzPaymentCard which implements the contract defined by the builder, which is ICard in our case. Here we can say that Abc and Xyz are responsible for building cards with magnetic strip and chip capabilities.
  1. using Builder.Design.Pattern.Contract;  
  2.   
  3. namespace Builder.Design.Pattern.Cards  
  4. {  
  5.     public class AbcPaymentCard : ICard  
  6.     {  
  7.         private Card _card = new Card();  
  8.   
  9.         public void AddChip()  
  10.         {  
  11.             _card.Add("AbcPaymentCard integrated with Chip Facility");  
  12.         }  
  13.   
  14.         public void AddMagneticStrip()  
  15.         {  
  16.             _card.Add("AbcPaymentCard integrated with Magnetic-Strip Facility");  
  17.         }  
  18.   
  19.         public Card GetCard()  
  20.         {  
  21.             return _card;  
  22.         }  
  23.     }  
  24. }  
  1. using Builder.Design.Pattern.Contract;  
  2.   
  3. namespace Builder.Design.Pattern.Cards  
  4. {  
  5.     public class XyzPaymentCard : ICard  
  6.     {  
  7.         private Card _card = new Card();  
  8.   
  9.         public void AddChip()  
  10.         {  
  11.             _card.Add("XyzPaymentCard integrated with Chip Facility");  
  12.         }  
  13.   
  14.         public void AddMagneticStrip()  
  15.         {  
  16.             _card.Add("XyzPaymentCard integrated with Magnetic-Strip Facility");  
  17.         }  
  18.   
  19.         public Card GetCard()  
  20.         {  
  21.             return _card;  
  22.         }  
  23.     }  
  24. }  
Below is Card class which is our product:
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace Builder.Design.Pattern  
  5. {  
  6.     public class Card  
  7.     {  
  8.         private List<string> _cards = new List<string>();  
  9.   
  10.         public void Add(string part)  
  11.         {  
  12.             _cards.Add(part);  
  13.         }  
  14.   
  15.         public void Show()  
  16.         {  
  17.             Console.WriteLine("--- Card Built ---");  
  18.             foreach (string part in _cards)  
  19.                 Console.WriteLine(part);  
  20.   
  21.             Console.WriteLine();  
  22.         }  
  23.     }  
  24. }  
Below is the BankA Class, which got cards from Abc and Xyz payment vendors and is providing services to customers. 
  1. using Builder.Design.Pattern.Contract;  
  2.   
  3. namespace Builder.Design.Pattern  
  4. {  
  5.     public class BankA  
  6.     {  
  7.         public void PrepareCard(ICard card)  
  8.         {  
  9.             card.AddMagneticStrip();  
  10.             card.AddChip();             
  11.         }  
  12.     }  
  13. }  
Below is the customer class, which will communicate to the bank for the cards. In general we can say that customers are visiting banks for payment cards.
  1. using Builder.Design.Pattern.Cards;  
  2. using Builder.Design.Pattern.Contract;  
  3. using System;  
  4.   
  5. namespace Builder.Design.Pattern  
  6. {  
  7.     class Customer  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             BankA bank = new BankA();  
  12.   
  13.             ICard cardTypeAbc = new AbcPaymentCard();  
  14.             bank.PrepareCard(cardTypeAbc);  
  15.             Card cardAbc = cardTypeAbc.GetCard();  
  16.             cardAbc.Show();  
  17.   
  18.             ICard cardTypeXyz = new XyzPaymentCard();  
  19.             bank.PrepareCard(cardTypeXyz);  
  20.             Card cardXyz = cardTypeXyz.GetCard();  
  21.             cardXyz.Show();  
  22.   
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
Below is the output snap of customer class:
 
Builder Design Pattern Using C# Sample
 

Summary

 
In this article we understood about the builder design pattern and how we can use it. This pattern is really useful when we need to implement complex objects.