# newer for poetry
curl -sSL https://install.python-poetry.org | POETRY_VERSION=1.2.1 python3 -
# install dependency
poetry install
poetry shell
# install pre-commit and run checks
pre-commit install
pre-commit run -a
The authors of the book called "gang of four" (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) investigated various common software design problems, and identified best-practice solutions that are commonly used in the software industry.
These common solutions were called design patterns.
A design pattern describes a basic approach to solving a particular design problem. It doesn't exist as a code library, or a set of detailed instructions for implementing a solution. Instead it describes the basic elements involved in the design, and how they relate to each other. It is up to the software designer to apply the pattern to their own software design.
In all, the authors identified 23 patterns, which they divided into 3 categories.
Creational patterns provide useful ways to create objects. These include:
- Factory Method (that creates an object whose type is determined by context).
- Builder (used to break down the creation of complex objects into separate steps).
- Singleton (used to ensure that only one object of a particular class can ever exist).
Structural patterns control the relationships between objects. These include:
- Adapter allows objects with incompatible interfaces to collaborate.
- Bridge lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.
- Composite (where an object delegates its behaviour to one or more other objects).
- Decorator (which is used to modify the functionality of an object without changing the object itself or its interface).
Behavioural patterns relate to the interactions between classes at runtime. These include:
- Observer (which allows objects to notified other object when their state changes).
- Strategy that allows alternate algorithms to be selected.
- Chain of responsibility is a behavioural design pattern. It is used to process command objects, where different types of command objects might need to be processed in different ways.
- Python Design Patterns: -03- The Factory
- simple_factory.py
- factory_method.py factory-method example
- abstract_factory.py abstract-factory example
''Bridge'' is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently.
- pazdera/bridge.py python 2 implementation
- Bridge in Python
- Bridge pattern in Python
``plantuml @startuml 'Class Diagram for Bridge Design Pattern
Website <|-- FreeWebsite Website <|-- PaidWebsite
Website : init(self, implementation) Website : show_page(self)
Implementation <|-- ImplementationA Implementation <|-- ImplementationB
Implementation : get_excerpt(self) Implementation : get_article(self) Implementation : get_ads(self):
@enduml ``