Design_patterns - kamialie/knowledge_corner GitHub Wiki

Design patterns

Python design patterns

Creational

Used to create objects in systematic way.

Polymorphism.

Factory

Factory pattern is used when user is uncertain about which class or type to use, and the decision will be made during runtime. Pattern provides a function or method, which provides necessary object, based on passed argument.

Abstract factory will be housing a concrete factory (fe, as an attribute), thus, making it easy to add more concrete factories later, while user will continue to use abstract factory.

Singleton

OOP way of providing global variables. Only one object is expected to be instantiated from a singleton class. Use case - information cache to be used my multiple objects.

In python modules act as a singleton. There is also a Borg design pattern.

Builder

Implemets "divide and concure strategy" by separating one complex object into multiple with their own constructors.

Roles:

  • director - responsible for final product
  • abstract builder - interface for builders
  • concrete builder - implementation
  • product

Prototype

Used when there is a need to create many identical objects. First, prototype object is created, then it is cloned, when a replica is needed.

Structural

Establishes relationships between components in certain configuration.

Inheritance.

Decorator

Add additional features to an existsing object without use of subclassing.

Proxy

Usefull when creating resource intensive object. The goal is to postpone the object creation unless absolutely necessary. Client interacts with a proxy which is responsible for creating this object.

Adapter

Used when there is a need to convert Class' interface to another that client expects. The problem arises when server and client are using different interfaces.

Composite

Tree-like structure that represents part-whole relationship. Example is Menu->Submenu->Submenu.

Bridge

There are 2 unrelated, parallel abstractions, one of which is implementaion specific, while another is implementation independent.

Behavioral

Represents best practices of objects interaction (defining a protocol).

Observer

Establishes one-to-many relationship between subject and observers. Observers shoulds be notified, when there is a change in object.

Implementation invovles abstract class with attach, detach and notify methods, which concrete object with inheret from.

Methods and their signatures.

Visitor

Allows adding new featuires to an existing class hierarchy without changing it.

Iterator

Provides sequential access to the elements of an aggregate object without exposing its underlying structure, thus, providing isolation, interface and tracking.

Strategy

Allows objects to dynamically change their behaviour, thus, providing the client with interchangeable algorithms.

Chain of responsibility

Decouples the requist and its processing. Current handler checks if it can handle the request and return a true value indicating that the request was handled.