PYTHON DESIGN PATTERN

 

PYTHON DESIGN PATTERN




Python Design Pattern-INTRODUCTION

  • The design pattern is a technique which used by the developer to solve the commonly occurring software design. In simple word, it is a predefine pattern to solve a recurring problem in the code.

  • These patterns are mainly designed based on requirements analysis.

  • The design pattern is a part of the software development. It is a general repeatable solution for the potential problem in software development.

  • We can follow the patterns details and apply a solution which suits our code.

  • For example - An algorithm is like a cooking recipe: we have a clear set of ingredients (or set of solutions) to cook something (problems or goals). On the other side, a pattern is like a blueprint: we can see what the result and its features are, but we can modify the order of implementation.


Configuration of Design Pattern

  • The documentation of design pattern is maintained in a way that focuses more on the technology that is used and in what ways. The following diagram explains the basic structure of design pattern documentation.



Pattern Name:-  It is used to define the pattern in a short and effective manner.

Intent/Motive:- It defines the goal or what the pattern does.

Applicability:- It defines all the possible areas where the pattern is applicable.

Participants and Consequences:- - It consists of classes and objects used in the design pattern with the list of consequences that exist with the pattern.


Why Python?

Python is an open source scripting language. It has libraries that support a variety of design patterns. The syntax of python is easy to understand and uses English keywords.


Advantages of Using Design Pattern


The advantages of using the design patterns are given below.

      All design patterns are language neutral.

      Patterns offer programmers to select a tried and tested solution for the specific problems.

      It consists of record of execution to decrease any technical risk to the projects.

      Patterns are easy to use and highly flexible.


Design Pattern in Python

Python is a high-level, open-source, and dynamic typed language. It has English-like syntax and easy to learn. It provides numerous libraries that support a variety of designs.

Some of the design patterns supported in python are:-

  • Model View Controller Pattern
  • Flyweight Pattern
  • Factory Pattern
  • Singleton Pattern
  • Object Oriented Pattern
  • Strategy Pattern
  • Command Pattern
  • Fascade Pattern
  • Observer Pattern
  • Adapter Pattern


What Constitutes a design pattern in Python?

  • Pattern Name

  • Intent
  • Aliases
  • Motivation
  • Sample Code
  • Constraints
  • Structure
  • Solution
  • Problem
  • Participants


Model View Controller Pattern 

  • The Model View Controller (MVC) design pattern specifies that an application consist of a data model, presentation information, and control information. The pattern requires that each of these be separated into different objects.

  • Model View Controller is the most commonly used design pattern. Developers find it easy to implement this design pattern.

  • MVC is more of an architectural pattern, but not for complete application. MVC mostly relates to the UI / interaction layer of an application. 



Architecture of Model View Controller Pattern



Structure of Model View Controller Pattern

  • Model - It consists of pure application logic, which interacts with the database. It includes all the information to represent data to the end user.
  • View - View represents the HTML files, which interact with the end user. It represents the model’s data to user.
  • Controller - It acts as an intermediary between view and model. It listens to the events triggered by view and queries model for the same.

Example of Design Pattern - SINGLETON PATTERN
(For demo click here)

Singleton Pattern restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the systems.

Singleton Pattern Python Code

class SingleTon(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance

o1 = SingleTon()
print("Object - 1 ==>", o1)
o1.data = 10

o2 = SingleTon()
print("Object - 2 ==>", o2)
print("Object - 2 data ==>", o2.data)
o2.data = 5

print("Object - 1 data ==>", o1.data)


Output:-









Comments