← Design Patterns learning pathDesign Patterns · Module 5 of 10
Decorator and Proxy
Add behaviour dynamically and control access.
📖 Key ideas
- Decorator adds behaviour to objects without changing their class.
- Example: adding milk and sugar to a coffee object.
- Python decorators (@) wrap functions.
- Proxy controls access to another object: caching, lazy loading, permissions.
- Both wrap an object with the same interface.
💡 Real-life example: Decorator is adding toppings to a pizza; Proxy is a receptionist deciding who meets the boss.
DecoratorAdds behaviour by wrapping
ProxyStand-in controlling access
Python @decoratorFunction wrapping syntax
Lazy loadingLoading only when needed