Builder Design Pattern Using Python Sample

Introduction

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

Where to use Builder Design Pattern?

 
When we have to build a complex object; i.e., the main class itself contain multiple objects which need to be created in order to accomplish creating the 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 we define a common class which is only responsible to build the required object.
 
Players in this pattern are:
  • Builder – one who will define contract to build different parts of one product
  • ConcreteBuilder – one who will implement builder contract
  • Product – This is a complex product which contains several sub parts.
  • ProductServer – the one who uses the product and provides its services to whoever needs it
We will see now how to implement it with an example.
 
Problem Definition
 
Build an application for payment card vendors which have to prepare magnetic strip and chip-based carsd so that card venders can provide these services to banks and banks can in turn provide these services to customers.
 
For the above problem we will use builder design pattern, below are the players:
  • Builder – AbstractCard
  • ConcreteBuilder – AbcPaymentCard, XyzPaymentCard
  • Product – Card
  • ProductServer – BankA 
Below is the AbstractCard contract which will define what objects/parts need to be built:
  1. from abc import ABC, abstractmethod  
  2.   
  3. class abstractCard(ABC):  
  4.  
  5.     @abstractmethod  
  6.     def addChip(self):  
  7.         pass  
  8.  
  9.     @abstractmethod  
  10.     def addMagneticStrip(self):  
  11.         pass  
  12.  
  13.     @abstractmethod  
  14.     def getCard(self):  
  15.         pass  
Below is AbcPaymentCard, XyzPaymentCard which implements the AbstractCard. Here we can say that Abc and Xyz are responsible for building cards with magnetic strip and chip capabilities.  
  1. from abstractCard import abstractCard  
  2. from Card import Card  
  3.   
  4.   
  5. class AbcPaymentCard(abstractCard):  
  6.   
  7.     __card = Card()  
  8.   
  9.     def addChip(self):  
  10.         self.__card.add('AbcPaymentCard integrated with Chip Facility')  
  11.   
  12.     def addMagneticStrip(self):  
  13.         self.__card.add('AbcPaymentCard integrated with Magnetic-Strip Facility')  
  14.   
  15.     def getCard(self) -> Card:  
  16.         return self.__card  
  1. from abstractCard import abstractCard  
  2. from Card import Card  
  3.   
  4.   
  5. class XyzPaymentCard(abstractCard):  
  6.   
  7.     __card = Card()  
  8.   
  9.     def addChip(self):  
  10.         self.__card.add('XyzPaymentCard integrated with Chip Facility')  
  11.   
  12.     def addMagneticStrip(self):  
  13.         self.__card.add('XyzPaymentCard integrated with Magnetic-Strip Facility')  
  14.   
  15.     def getCard(self) -> Card:  
  16.         return self.__card  
Below is card class, which is our product:
  1. class Card:  
  2.     _cards = []  
  3.   
  4.     def add(self, part: str):  
  5.         self._cards.append(part)  
  6.   
  7.     def show(self):  
  8.         print('Card Built')  
  9.         for card in self._cards:  
  10.             print(card)  
  11.         print()  
Below is the BankA Class, which got cards from Abc and Xyz payment Vendors and are providing services to their customers:
  1. from abstractCard import abstractCard   
  2.   
  3. class BankA:  
  4.   
  5.     def prepareCard(self, card: abstractCard):  
  6.         card.addMagneticStrip()  
  7.         card.addChip()  
Below is the customer class, which will communicate with the bank for card. In general we can say that customers are  visiting banks for payment cards.
  1. from BankA import BankA  
  2. from AbcPaymentCard import AbcPaymentCard  
  3. from XyzPaymentCard import XyzPaymentCard  
  4. from abstractCard import abstractCard  
  5.   
  6. class Customer:  
  7.   
  8.     def MainFunc(self):  
  9.         bank = BankA()  
  10.         cardTypeAbc: abstractCard = AbcPaymentCard()  
  11.         bank.prepareCard(cardTypeAbc)  
  12.         cardAbc = cardTypeAbc.getCard()  
  13.   
  14.         cardTypeXyz: abstractCard = XyzPaymentCard()  
  15.         bank.prepareCard(cardTypeXyz)  
  16.         cardXyz = cardTypeXyz.getCard()  
  17.         cardXyz.show()  
  18.   
  19.   
  20. if __name__ == "__main__":  
  21.     cs = Customer()  
  22.     cs.MainFunc()  
Below is the output snap of customer class,
 
Builder Design Pattern Using Python 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.


Recommended Free Ebook
Similar Articles