Proxy, Facade and Adapter Pattern using Python

Design Patterns for Beginners with real-world Examples

Design Patterns for Beginners with real-world Examples

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

  • 0:00:00 Introduction
  • 0:01:40 What are Design Patterns?
  • 0:04:15 How to Take This Course
  • 0:05:50 The Essentials
  • 0:06:53 Getting Started with Java
  • 0:09:23 Classes
  • 0:13:34 Coupling
  • 0:15:34 Interfaces
  • 0:21:17 Encapsulation
  • 0:26:25 Abstraction
  • 0:30:33 Inheritance
  • 0:32:55 Polymorphism
  • 0:36:42 UML
  • 0:40:52 Memento Pattern
  • 0:42:43 Solution
  • 0:48:31 Implementation
  • 0:54:22 State Pattern
  • 0:59:46 Solution
  • 1:02:59 Implementation
  • 1:09:31 Abusing the Design Patterns
  • 1:11:18 Abusing the State Pattern

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!

Java Design Patterns for Beginners - Design Patterns in Java

Java Design Patterns for Beginners - Design Patterns in Java

"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:

  • 00:32 Why do we need Design Patterns?
  • 01:35 Structure of Design Patterns
  • 03:56 Types of Design Patterns
  • 04:15 Creational Design Patterns
  • 04:45 Factory Design Pattern
  • 06:35 Singleton Design Design Pattern
  • 10:10 Structural Design Patterns
  • 10:47 Facade Design Pattern
  • 12:14 Adapter Design Pattern
  • 14:15 Behavioral Design Patterns
  • 14:52 Strategy Design Pattern
  • 16:25 Command Design Pattern
  • 18:15 Observer Design Pattern
  • 19:37 JEE Design Patterns
  • 20:14 MVC Design Pattern
  • 22:00 DAO Design Pattern
  • 23:20 Overview of Design Patterns