A design pattern is a general solution to a commonly occurring problem in software development. This documentation topic is specifically aimed at providing examples of common design patterns in Python.
Introduction to design patterns and Singleton Pattern
Design Patterns provide solutions to the commonly occurring problems in software design. The design patterns were first introduced by GoF(Gang of Four) where they described the common patterns as problems which occur over and over again and solutions to those problems.
Design patterns have four essential elements:
The pattern name is a handle we can use to describe a design problem, its solutions, and consequences in a word or two.
The problem describes when to apply the pattern.
The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations.
The consequences are the results and trade-offs of applying the pattern.
Advantages of design patterns:
They are reusable across multiple projects.
The architectural level of problems can be solved
They are time-tested and well-proven, which is the experience of developers and architects
They have reliability and dependence
Design patterns can be classified into three categories:
Creational Pattern - They are concerned with how the object can be created and they isolate the details of object creation.
Structural Pattern - They design the structure of classes and objects so that they can compose to achieve larger results.
Behavioral Pattern - They are concerned with interaction among objects and responsibility of objects.
It is a type of creational pattern which provides a mechanism to have only one and one object of a given type and provides a global point of access.
e.g. Singleton can be used in database operations, where we want database object to maintain data consistency.
We can implement Singleton Pattern in Python by creating only one instance of Singleton class and serving the same object again.
Note that in languages like C++ or Java, this pattern is implemented by making the constructor private and creating a static method that does the object initialization. This way, one object gets created on the first call and class returns the same object thereafter. But in Python, we do not have any way to create private constructors.
Factory pattern is also a Creational pattern. The term factory means that a class is responsible for creating objects of other types. There is a class that acts as a factory which has objects and methods associated with it. The client creates an object by calling the methods with certain parameters and factory creates the object of the desired type and return it to the client.
MusicFactory is the factory class here that creates either an object of type Mp3 or Ogg depending on the choice user provides.
Proxy object is often used to ensure guarded access to another object, which internal business logic we don't want to pollute with safety requirements.
Suppose we'd like to guarantee that only user of specific permissions can access resource.
Proxy definition: (it ensure that only users which actually can see reservations will be able to consumer reservation_service)
we're avoiding any changes in ReservationService when access restrictions are changed.
we're not mixing business related data (date_from, date_to, reservations_count) with domain unrelated concepts (user permissions) in service.
Consumer (StatsService) is free from permissions related logic as well
Proxy interface is always exactly the same as the object it hides, so that user that consumes service wrapped by proxy wasn't even aware of proxy presence.
This design pattern is called Strategy Pattern.
It is used to define a family of algorithms, encapsulates each
one, and make them interchangeable. Strategy
design pattern lets an algorithm vary independently
from clients that use it.
For example, animals can "walk" in many different ways. Walking could be considered a strategy that is implemented by different types of animals:
Running this example would produce the following output:
Note that in languages like C++ or Java, this pattern is implemented using an abstract class or an interface to define a a strategy. In Python it makes more sense to just define some functions externally that can be added dynamically to a class using types.MethodType.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0