Builder Design Pattern in Python

Builder Design Pattern in Python

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

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,

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 Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language