| 01 | Protocols and duck typing | code should accept types it has never heard of | call methods by name; any object that has them fits | json.load, collections.abc | a function should work with types you don’t own | built in; typing.Protocol to check it |
|---|
| 02 | Adapter | an object does the job behind the wrong interface | a wrapper translates calls in and results out | requests, asgiref | a library’s interface doesn’t match your code | a small class, or a function |
|---|
| 03 | Facade | the common case of a subsystem takes many calls | one function makes the calls in order and cleans up | subprocess.run, requests.get | callers repeat the same sequence and cleanup | a function |
|---|
| 04 | Decorator | behaviours combine and subclasses would multiply | wrap an object in one with the same methods plus one job | io (open), Werkzeug ProxyFix | behaviours stack in any order | a class that forwards with __getattr__ |
|---|
| 05 | Function decorators | the same code runs around many functions | replace the function with a wrapper that calls it | tenacity, functools.lru_cache | retry, cache, time or check many functions | @ syntax and functools.wraps |
|---|
| 06 | Proxy | the real object is per request, lazy or remote | a stand-in with its interface finds the real one on each use | Flask request, multiprocessing | callers shouldn’t care where the object comes from | __getattr__ plus the special methods |
|---|
| 07 | Descriptors | an attribute must run code when read or set | an object on the class with __get__ and __set__ | Django fields, cached_property | many attributes share one rule | property, or a class with __get__ |
|---|
| 08 | Composite | items nest to any depth | containers and items share one method | Click Group, ast | trees of commands, nodes or widgets | a class with a children list |
|---|
| 09 | Mixins | unrelated classes need the same slice of behaviour | a small base class hooks a shared method and calls super() | Django auth mixins, socketserver | one behaviour attaches to a method many classes have | multiple inheritance plus super() |
|---|
| 10 | Bridge | a hierarchy varies in two directions | two hierarchies joined by one reference | logging, SQLAlchemy dialects | subclass names start combining two ideas | an attribute holding the other object |
|---|
| 11 | Flyweight | many objects repeat a few immutable values | a factory returns one shared object per value | enum, pandas Categorical | memory goes to millions of equal objects | sys.intern, Enum, functools.cache |
|---|