python-design-patterns icon indicating copy to clipboard operation
python-design-patterns copied to clipboard

Learning design patterns with Jungwoo Ryoo

Python: Design Patterns

Design Patterns

  • Well-known solutions to recurring problems (same problem occurring over and over again)
  • Widely accepted solutions by the software development community
  • No need to re-invent the wheel

Creational Patterns

  1. [x] Factory - object specializing in creating other objects
    • when not sure what type of object will be needed
    • decisions to be made at runtime regarding what classes to use
  2. [x] Abstract factory - creates a factory of related objects without explicitly specifying their classes
    • useful when delivering family of related objects
    • don't need to know which family it is until runtime
    • relations: factory and singleton (concrete factory are often made as it)
  3. [x] Singleton - provides global variable object
    • allows for only one instance of a class
    • keeping an information cache: no need to retrieve information from original source every time when it's needed
  4. [x] Builder - solution for building complex object
    • doesn't rely on polymorphism (unlike factory and abstract factory)
    • example: before building a car, car parts (e.g. tyres, engine, etc.) need to be build and assembled
  5. [x] Prototype - clones object according to a prototypical instance
    • problem: creating many identical objects individually could be "expensive"
    • alternative: cloning instead of creation
    • solution: create prototype once, clone it every time when needed
    • relations: abstract factory

Structural Patterns

  1. [x] Decorator - adds additional feature to the existing object dynamically without using subclassing
    • relations: adapter, composite, strategy
  2. [x] Proxy - helps with creating an object which is resource intensive
    • relations: adapter, decorator
  3. [x] Adapter - converts the interface of the class into another one
    • e.g. unifies method names
    • relations: bridge, decorator
  4. [x] Composite - composes objects into tree structures to represent part-whole hierarchies
    • e.g. menu(submenu1, submenu2) - submenu1(sub11_submenu1, sub12_submenu1) - ...
    • relations: decorator, iterator, visitor
  5. [x] Bridge - decouples an abstraction from its implementation so that the two can vary independently
    • publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy
    • you can split the hierarchy of interface and class
    • relations: abstract factory, adapter

Behavioral Patterns

  1. [x] Observer - establishes an one-to-many relation between a subject and multiple observers
    • problem: subject needs to be monitored, and observers need to be notified on subject's change
    • relations: singleton
  2. [x] Visitor - allows adding new features to the existing class hierarchy without changing it
    • can provide operations on a composite objects
  3. [x] Iterator - sequentially accesses the elements of a collection
    • relations: composite
  4. [x] Strategy - encapsulates an algorithm inside a class
    • in other words: pass a method as an argument
  5. [x] Chain of Responsibility - a way of passing a request between a chain of objects
    • relations: composite

See also