Design Patterns


Introduction

Design patterns are used to represent the pattern used by developers to create software or web application. These patterns are selected based on the requirement analysis. The patterns describe the solution to the problem, when and where to apply the solution and the consequences of the implementation.

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. Python provides support for the list of design patterns some of them are as mentioned below.

  1. Singleton pattern
  2. Factory pattern
  3. Builder pattern
  4. Facade pattern
  5. Command pattern
  6. Prototype pattern
  7. Proxy pattern
  8. Chain of Responsibility pattern
  9. Abstract Factory pattern
  10. Observer Pattern
  11. Object Oriented Pattern

Benefits of using Design Patterns :

  1. Patterns provide developer a selection of tried and tested solutions for the specified problems.
  2. All design patterns are language neutral.
  3. Patterns help to achieve communication and maintain well documentation.
  4. It includes a record of accomplishment to reduce any technical risk to the project.
  5. Design patterns are highly flexible to use and easy to understand.

Factory pattern

The factory pattern comes under the creational patterns list category. It provides one of the best ways to create an object. In factory pattern, objects are created without exposing the logic to client and referring to the newly created object using a common interface.

Factory patterns are implemented in Python using factory method. When a user calls a method such that we pass in a string and the return value as a new object is implemented through factory method. The type of object used in factory method is determined by string which is passed through method.

In our calculator program, a factory method is utilized in the calculation.py file within the calculation's directory as the "create" method that allows for a calculation object (Addition, Subtraction, Division, Multiplication) to be made with argument values.

factory factory pattern

Command Pattern

Command Pattern adds a level of abstraction between actions and includes an object, which invokes these actions.

In this design pattern, client creates a command object that includes a list of commands to be executed. The command object created implements a specific interface.

Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations.

In our calculator program, a command pattern is utilized in the calculator.py file within the calculator directory. Basically in Calculator class, we are not defining any method. We are just calling the methods which are created in other classes.

mySpace logo