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:
- namespace Builder.Design.Pattern.Contract
- {
- public interface ICard
- {
- void AddMagneticStrip();
- void AddChip();
- Card GetCard();
- }
- }
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.
- using Builder.Design.Pattern.Contract;
-
- namespace Builder.Design.Pattern.Cards
- {
- public class AbcPaymentCard : ICard
- {
- private Card _card = new Card();
-
- public void AddChip()
- {
- _card.Add("AbcPaymentCard integrated with Chip Facility");
- }
-
- public void AddMagneticStrip()
- {
- _card.Add("AbcPaymentCard integrated with Magnetic-Strip Facility");
- }
-
- public Card GetCard()
- {
- return _card;
- }
- }
- }
- using Builder.Design.Pattern.Contract;
-
- namespace Builder.Design.Pattern.Cards
- {
- public class XyzPaymentCard : ICard
- {
- private Card _card = new Card();
-
- public void AddChip()
- {
- _card.Add("XyzPaymentCard integrated with Chip Facility");
- }
-
- public void AddMagneticStrip()
- {
- _card.Add("XyzPaymentCard integrated with Magnetic-Strip Facility");
- }
-
- public Card GetCard()
- {
- return _card;
- }
- }
- }
Below is Card class which is our product:
- using System;
- using System.Collections.Generic;
-
- namespace Builder.Design.Pattern
- {
- public class Card
- {
- private List<string> _cards = new List<string>();
-
- public void Add(string part)
- {
- _cards.Add(part);
- }
-
- public void Show()
- {
- Console.WriteLine("--- Card Built ---");
- foreach (string part in _cards)
- Console.WriteLine(part);
-
- Console.WriteLine();
- }
- }
- }
Below is the BankA Class, which got cards from Abc and Xyz payment vendors and is providing services to customers.
- using Builder.Design.Pattern.Contract;
-
- namespace Builder.Design.Pattern
- {
- public class BankA
- {
- public void PrepareCard(ICard card)
- {
- card.AddMagneticStrip();
- card.AddChip();
- }
- }
- }
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.
- using Builder.Design.Pattern.Cards;
- using Builder.Design.Pattern.Contract;
- using System;
-
- namespace Builder.Design.Pattern
- {
- class Customer
- {
- static void Main(string[] args)
- {
- BankA bank = new BankA();
-
- ICard cardTypeAbc = new AbcPaymentCard();
- bank.PrepareCard(cardTypeAbc);
- Card cardAbc = cardTypeAbc.GetCard();
- cardAbc.Show();
-
- ICard cardTypeXyz = new XyzPaymentCard();
- bank.PrepareCard(cardTypeXyz);
- Card cardXyz = cardTypeXyz.GetCard();
- cardXyz.Show();
-
- Console.ReadLine();
- }
- }
- }
Below is the output snap of customer class:
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.