Imagine that you want to build a house. You have a final image of the appearance of this house in your mind and based on this general view of the house, you start building the house and after a while, you see that if you continue in this way, your dream house will look like that. The appearance you imagined is not made. Where is the problem?
you guessed right. The problem is that there is no predetermined plan for building this house. The existence of a map and pattern can help you in building a house. Also, this template can be reused. This means that other houses should be built based on this model.
It is the same in software development, a series of predetermined design patterns can be used for software development or design. For example, adding a new page to a website does not require redesigning everything from scratch. You can follow the pattern and steps of creating other pages and use the same pattern again.
What you read is the basic and simplified concept of design patterns. It is common to use design patterns in various object-oriented programming languages. In this article, we focus on design patterns in Python.
Table of Contents
What are design patterns in programming?
Design patterns are general solutions to common problems that occur in software design. These patterns usually represent relationships and interactions between classes or objects. The idea of using design patterns is to speed up the programming process by providing tested and proven design and development paradigms. Design patterns are programming language-independent strategies for solving common problems. By using design patterns, you can make your code more flexible, reusable, and maintainable.
The use of design patterns in programming is not mandatory. Design patterns are not intended for project development, but for solving problems that are common in different projects. There are many different types of design patterns. To know which pattern to use, you should try to understand the types of design patterns and their goals.
Advantages of using design patterns in Python
Design patterns can significantly improve the software development process. Among the most important advantages of using design patterns in programming, including design patterns in Python, the following can be mentioned:
- Using design patterns reduces code complexity and the possibility of errors.
- The use of design patterns increases the speed of software development.
- Speeding up the software development process
- Reduce the number of lines of code
- Ensuring the health of the code
- Anticipate future problems
- Reusability
The difference between the design pattern and the algorithm
Usually, the concept of pattern is confused with the algorithm. Both concepts describe solutions to problems. But an algorithm defines a clear set of actions that ultimately lead to a specific goal, while a pattern is a high-level description of a solution. The code snippet of a pattern applied to two different programs may be different, while the algorithm is not.
Types of design patterns in Python
In general, design patterns are divided into three general categories:
- Creational
- Structural
- Behavioral
In the rest of this article, we will examine each type of design pattern in Python and we will introduce some of the most used design patterns.
Creational
Creational design patterns deal with instantiating classes or objects. Creational design patterns are further divided into Class-creational patterns and Object-creational patterns. Class-creational patterns use inheritance in the instantiation process, while Object-creational patterns use delegation of tasks between objects to perform work.
Creational design patterns include:
- Factory method: Provides an interface for creating objects in a superclass, but allows subclasses to change the type of objects that are created.
- Abstract Factory: Allows you to generate families of related objects without specifying their classes.
- Builder: Allows you to build complex objects. This pattern allows you to generate different types and representations of the same object using the same code.
- Singleton: Singleton restricts a class from having more than one instance and ensures a universal access point to this instance.
Structural
These design patterns are about organizing different classes and objects to form larger structures and provide new functionality.
Structure design patterns are:
- Adapter Method: Allows objects with incompatible interfaces to work together.
- Bridge Method: Allows you to split a large class or a set of closely related classes into two separate hierarchies. which can develop independently of each other.
- Composite Method: Allows you to write objects in tree structures and then treat these structures as single objects.
- Decorator Method: This pattern allows the programmer to add new functionality to an existing object without changing its structure.
- Facade Method: Provides a simple interface to a library, framework, or any other complex assembly.
- Proxy Method: Allows you to provide a proxy or placeholder for another object. A proxy controls access to the parent object and allows you to do something before or after the request reaches the parent object.
- FlyWeight Method: Allows you to fit more objects into the amount of available RAM by sharing common parts of the state between multiple objects instead of keeping all the data in each object.
Behavioral
Patterns are about identifying common communication patterns between objects. These patterns deal with algorithms and assigning responsibilities between objects.
Behavior patterns include:
- Chain of Responsibility Method: Allows you to send requests in a chain of controllers. The controller, after receiving the request, processes it or forwards it to the next controller in the chain.
- Command Method: Converts a request into an independent object that contains all the information about the request. This transformation allows you to pass requests as method arguments, delay or queue the execution of a request, and support non-committable operations.
- Iterator Method: Allows you to iterate over the elements of collections without going into their deep details. This pattern also provides a way to sequentially access the features of a complex data structure without having to repeat them.
- Mediator Method: Allows you to reduce messy dependencies between objects.
- Memento Method: Provides the ability to restore an object to a previous state, allowing you to save and restore a previous version of an object.
- Observer Method: Allows you to define or create a shared mechanism to send notifications to multiple objects about any new event that happens to a particular thing.
- State Method: When there is a change in the internal state of an object, it allows it to change its behavior.
- Strategy Method: Allows you to define a whole set of algorithms, encapsulate each one and put each of them in separate classes, and also allows you to exchange objects there.
- Template Method: Defines the skeleton and basis of an algorithm in the superclass but allows subclasses to override specific steps of the algorithm without changing its structure.
- Visitor Method: Allows you to separate algorithms from the objects they operate on.