Design Patterns for Beginners with real-world Examples. When do you use Design Patterns? How do you implement different Design Patterns in Java? What are Design Patterns? Why do you use Design Patterns? What are the different types of Design Patterns? When do you use Design Patterns? What are the real-world examples for Design Patterns.
Design Patterns tutorial explained in simple words using real-world examples.
TABLE OF CONTENT
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:
We will see now how to implement it with an example.
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:
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.
"Java Design Patterns for Beginners - Design Patterns in Java - Design Patterns Tutorial" will provide you with detailed knowledge about Java Design Patterns and along with it. Why do we need Design Patterns? Structure of Design Patterns. Types of Design Patterns. Creational Design Patterns. Factory Design Pattern. Overview of Design Patterns
This Edureka video on "Java Design Patterns for Beginners - Design Patterns in Java" will provide you with detailed knowledge about Java Design Patterns and along with it, This video will also cover some real-time examples of some important Design Patterns in Java, in order to provide you with a deep understanding about their functionality. This video will cover the following topics: