Builder Design Pattern in Python

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:

from abc import ABC, abstractmethod  
  
class abstractCard(ABC):  
 
    @abstractmethod  
    def addChip(self):  
        pass  
 
    @abstractmethod  
    def addMagneticStrip(self):  
        pass  
 
    @abstractmethod  
    def getCard(self):  
        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.

from abstractCard import abstractCard  
from Card import Card  
  
  
class AbcPaymentCard(abstractCard):  
  
    __card = Card()  
  
    def addChip(self):  
        self.__card.add('AbcPaymentCard integrated with Chip Facility')  
  
    def addMagneticStrip(self):  
        self.__card.add('AbcPaymentCard integrated with Magnetic-Strip Facility')  
  
    def getCard(self) -> Card:  
        return self.__card  
from abstractCard import abstractCard  
from Card import Card  
  
  
class XyzPaymentCard(abstractCard):  
  
    __card = Card()  
  
    def addChip(self):  
        self.__card.add('XyzPaymentCard integrated with Chip Facility')  
  
    def addMagneticStrip(self):  
        self.__card.add('XyzPaymentCard integrated with Magnetic-Strip Facility')  
  
    def getCard(self) -> Card:  
        return self.__card  

Below is card class, which is our product:

class Card:  
    _cards = []  
  
    def add(self, part: str):  
        self._cards.append(part)  
  
    def show(self):  
        print('Card Built')  
        for card in self._cards:  
            print(card)  
        print()  

Below is the BankA Class, which got cards from Abc and Xyz payment Vendors and are providing services to their customers:

from abstractCard import abstractCard   
  
class BankA:  
  
    def prepareCard(self, card: abstractCard):  
        card.addMagneticStrip()  
        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.

from BankA import BankA  
from AbcPaymentCard import AbcPaymentCard  
from XyzPaymentCard import XyzPaymentCard  
from abstractCard import abstractCard  
  
class Customer:  
  
    def MainFunc(self):  
        bank = BankA()  
        cardTypeAbc: abstractCard = AbcPaymentCard()  
        bank.prepareCard(cardTypeAbc)  
        cardAbc = cardTypeAbc.getCard()  
  
        cardTypeXyz: abstractCard = XyzPaymentCard()  
        bank.prepareCard(cardTypeXyz)  
        cardXyz = cardTypeXyz.getCard()  
        cardXyz.show()  
  
  
if __name__ == "__main__":  
    cs = Customer()  
    cs.MainFunc()  

Below is the output snap of customer class,

This is image title

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.

Thank you!

#python #developer #programming #design #tutorial

Builder Design Pattern in Python
1 Likes33.00 GEEK