--- url: "https://refactoring.guru/design-patterns/python" titulek: "Design Patterns in Python" h1: "Design Patterns in Python" popis: "The catalog of annotated code examples of all design patterns, written in Python." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/python" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:22:24" znaku: 7482 slov: 834 kodovani: "utf-8" sha256: "4b88f5433bc37cfcb7eef93a4ef40b1659adad272419f90c3e2856d759d9195b" --- # Design Patterns in Python ## The Catalog of **Python** Examples #### Creational Patterns #### Abstract Factory Lets you produce families of related objects without specifying their concrete classes. [Main article](/design-patterns/abstract-factory) [Usage in Python](/design-patterns/abstract-factory/python/example#lang-features) [Code example](/design-patterns/abstract-factory/python/example#example-0) #### Builder Lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code. [Main article](/design-patterns/builder) [Usage in Python](/design-patterns/builder/python/example#lang-features) [Code example](/design-patterns/builder/python/example#example-0) #### Factory Method Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. [Main article](/design-patterns/factory-method) [Usage in Python](/design-patterns/factory-method/python/example#lang-features) [Code example](/design-patterns/factory-method/python/example#example-0) #### Prototype Lets you copy existing objects without making your code dependent on their classes. [Main article](/design-patterns/prototype) [Usage in Python](/design-patterns/prototype/python/example#lang-features) [Code example](/design-patterns/prototype/python/example#example-0) #### Singleton Lets you ensure that a class has only one instance, while providing a global access point to this instance. [Main article](/design-patterns/singleton) [Usage in Python](/design-patterns/singleton/python/example#lang-features) [Naïve Singleton](/design-patterns/singleton/python/example#example-0) [Thread-safe Singleton](/design-patterns/singleton/python/example#example-1) #### Structural Patterns #### Adapter Allows objects with incompatible interfaces to collaborate. [Main article](/design-patterns/adapter) [Usage in Python](/design-patterns/adapter/python/example#lang-features) [Code example](/design-patterns/adapter/python/example#example-0) #### 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. [Main article](/design-patterns/bridge) [Usage in Python](/design-patterns/bridge/python/example#lang-features) [Code example](/design-patterns/bridge/python/example#example-0) #### Composite Lets you compose objects into tree structures and then work with these structures as if they were individual objects. [Main article](/design-patterns/composite) [Usage in Python](/design-patterns/composite/python/example#lang-features) [Code example](/design-patterns/composite/python/example#example-0) #### Decorator Lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors. [Main article](/design-patterns/decorator) [Usage in Python](/design-patterns/decorator/python/example#lang-features) [Code example](/design-patterns/decorator/python/example#example-0) #### Facade Provides a simplified interface to a library, a framework, or any other complex set of classes. [Main article](/design-patterns/facade) [Usage in Python](/design-patterns/facade/python/example#lang-features) [Code example](/design-patterns/facade/python/example#example-0) #### Flyweight Lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object. [Main article](/design-patterns/flyweight) [Usage in Python](/design-patterns/flyweight/python/example#lang-features) [Code example](/design-patterns/flyweight/python/example#example-0) #### Proxy Lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object. [Main article](/design-patterns/proxy) [Usage in Python](/design-patterns/proxy/python/example#lang-features) [Code example](/design-patterns/proxy/python/example#example-0) #### Behavioral Patterns #### Chain of Responsibility Lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain. [Main article](/design-patterns/chain-of-responsibility) [Usage in Python](/design-patterns/chain-of-responsibility/python/example#lang-features) [Code example](/design-patterns/chain-of-responsibility/python/example#example-0) #### Command 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. [Main article](/design-patterns/command) [Usage in Python](/design-patterns/command/python/example#lang-features) [Code example](/design-patterns/command/python/example#example-0) #### Iterator Lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.). [Main article](/design-patterns/iterator) [Usage in Python](/design-patterns/iterator/python/example#lang-features) [Code example](/design-patterns/iterator/python/example#example-0) #### Mediator Lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object. [Main article](/design-patterns/mediator) [Usage in Python](/design-patterns/mediator/python/example#lang-features) [Code example](/design-patterns/mediator/python/example#example-0) #### Memento Lets you save and restore the previous state of an object without revealing the details of its implementation. [Main article](/design-patterns/memento) [Usage in Python](/design-patterns/memento/python/example#lang-features) [Code example](/design-patterns/memento/python/example#example-0) #### Observer Lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they're observing. [Main article](/design-patterns/observer) [Usage in Python](/design-patterns/observer/python/example#lang-features) [Code example](/design-patterns/observer/python/example#example-0) #### State Lets an object alter its behavior when its internal state changes. It appears as if the object changed its class. [Main article](/design-patterns/state) [Usage in Python](/design-patterns/state/python/example#lang-features) [Code example](/design-patterns/state/python/example#example-0) #### Strategy Lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable. [Main article](/design-patterns/strategy) [Usage in Python](/design-patterns/strategy/python/example#lang-features) [Code example](/design-patterns/strategy/python/example#example-0) #### Template Method Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. [Main article](/design-patterns/template-method) [Usage in Python](/design-patterns/template-method/python/example#lang-features) [Code example](/design-patterns/template-method/python/example#example-0) #### Visitor Lets you separate algorithms from the objects on which they operate. [Main article](/design-patterns/visitor) [Usage in Python](/design-patterns/visitor/python/example#lang-features) [Code example](/design-patterns/visitor/python/example#example-0)