Home » Python Design Patterns

Python Design Patterns

by Online Tutorials Library

Python Design Pattern

This tutorial will discuss what Design Pattern is and how we can implement using the Python programming language. We will also explain several types of design patterns. We will explore the different approaches to solve the Python problems. Python is a high-level, dynamic type language and popularly used in almost every possible technical domain.

What is Design Pattern?

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.

We may often confuse the patterns and algorithm, but both are separate approaches to solve repetitive problems. Algorithms generally define the clear set of the solution that can be implemented in some problems, where the patters are high-level description of the solution.

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

In the below diagram, we describe the basic structure of the design pattern documentation. It is focused on what technology we are using to solve the problems and in what ways.

Python Design Pattern

In the above diagram-

Pattern Name – It is used to define the pattern in a shortly and effectively.

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.

History of Patterns

Design patterns are the set of the solution of common problems in object-oriented design. When the problem’s solution is repeated again and again in the various projects, someone eventually puts a name and defines the solution in detail. That is how the pattern gets recognized.

Christopher Alexander has described the concept of the pattern for the first time in the book named A Pattern Language: Towns, Building, and Construction.

This book defines a ‘language’ for designing the urban environment. The language is nothing but the patterns.

The four authors: Erich Gamma, John, Vlissiders, Ralph Johnson, and Richard Helm, were picked the idea of a pattern language. Later, they published the book named Design Patterns: Elements of Reusable Object-Oriented Software. This book contains the concept of design patterns using the programming language.

The book featured the 23 useful various problems of object-oriented designs; It gained the much popularity among the programmers and became the best seller book very quickly.

Interesting Fact – This book has a very long name so people started to call it “The book of gang of four” which was soon summarized to simply “The GoF book”.

Many other object-oriented patterns are discovered after this book. Very soon, the pattern approach became very famous in the programming fields. There are many others patterns available apart from object-oriented design as well.

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

We are all familiar with Python’s feature; if someone does not, let’s have a brief introduction – 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.

We are listed below the design patterns that are supported by Python. We will use these design patterns in our tutorial.

  • Model View Controller Pattern
  • Flyweight Pattern
  • Factory pattern
  • Singleton pattern
  • Object Oriented Pattern
  • Strategy Pattern
  • Command Pattern
  • Chain of Responsibility Pattern
  • Abstract Factory Pattern
  • Proxy Pattern
  • Facade Pattern
  • Observer Pattern
  • Prototype Pattern
  • Template Pattern
  • Adapter Pattern
  • Builder Pattern
  • Prototype Pattern
  • Decorator Pattern
  • State Pattern

Importance of Learn Design Pattern

Many of the software developers might work for many years without knowing about any single pattern. It can also happen we might be implementing a pattern without even knowing it. So, here the question arises, why should we learn the design pattern? Let’s look at the following points, which light up the importance of design patterns in development.

  • Design patterns have the predefined set of tried and tested solutions to a common problem encountered while developing software. If we know about the design pattern, then we can apply the solution without wasting time. It also teaches us how to solve the problem using the principle of object-oriented design.
  • Design pattern also enhances the common understanding between the developer and their teammates. Suppose there is a problem in the code, and you can say “Use Singleton for that,” and everyone can understand if he/she knows the design pattern and its name.
  • Design patterns are also useful for the learning purpose because they introduce the common problem that we may have ignored. They also allow thinking that area that may not have had the hand-on experience.

We will explore more about the design patterns in the upcoming tutorial.


You may also like