python-design-patterns
python-design-patterns copied to clipboard
Learning design patterns with Jungwoo Ryoo
Python: Design Patterns
- [x] Examples from Python: Design Patterns by Jungwoo Ryoo
- [ ] Examples from Python: Advanced Design Patterns by Jungwoo Ryoo
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
- [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
- [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)
- [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
- [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
- [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
- [x] Decorator - adds additional feature to the existing object dynamically without using subclassing
- relations: adapter, composite, strategy
- [x] Proxy - helps with creating an object which is resource intensive
- relations: adapter, decorator
- [x] Adapter - converts the interface of the class into another one
- e.g. unifies method names
- relations: bridge, decorator
- [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
- [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
- [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
- [x] Visitor - allows adding new features to the existing class hierarchy without changing it
- can provide operations on a composite objects
- [x] Iterator - sequentially accesses the elements of a collection
- relations: composite
- [x] Strategy - encapsulates an algorithm inside a class
- in other words: pass a method as an argument
- [x] Chain of Responsibility - a way of passing a request between a chain of objects
- relations: composite
See also
- The Little Book of Python Anti-Patterns by @QuantifiedCode
- The Hitchhiker's Guide to Python by @realpython
- Dive Into Python 3 by Mark Pilgrim
- python-patterns (a collection of design patterns and idioms in Python) by @faif
- Python Design Patterns: For Sleek And Fashionable Code by Andrei Boyanov
- A detailed description of design patterns by SourceMaking
- What is the difference between the Bridge pattern and the Strategy pattern? (Stack Overflow)
- formal interfaces in Python by William Murphy
- abstract base classes in Python by Isaac Rodriguez