Proxy Pattern — ELI5
Imagine a celebrity has a personal assistant. You don’t talk to the celebrity directly — you talk to the assistant first. The assistant checks your appointment, decides if the request makes sense, and either passes it along or politely turns you away.
That assistant is a Proxy.
In Python, a proxy is an object that stands in front of another object and controls access to it. The proxy looks and acts like the real thing from the outside, but behind the scenes it can do extra work — checking permissions, caching results, or delaying expensive operations until they’re actually needed.
Here’s where it gets practical. Say your app loads a huge image file. Loading it takes three seconds. But what if the user never scrolls down to see it? A proxy can pretend to be the image, and only load the actual file when someone tries to display it. Until then, it saves time and memory by doing nothing.
Proxies show up in everyday life too. A credit card is a proxy for your bank account — the store doesn’t access your account directly. A landlord’s property manager is a proxy — tenants deal with the manager, not the owner.
The magic is that the rest of your code doesn’t know it’s talking to a proxy. It uses the same methods, gets the same results. The proxy is invisible to everything except the thing it’s protecting.
The one thing to remember: A proxy stands between your code and a real object, adding control — like security, caching, or lazy loading — without changing how the rest of the code works.
See Also
- Python Adapter Pattern How Python's Adapter Pattern works like a travel power plug — making incompatible things work together.
- Python Bridge Pattern Why separating what something does from how it does it keeps your Python code from becoming a tangled mess.
- Python Builder Pattern Why building complex Python objects step by step beats cramming everything into one giant constructor.
- Python Composite Pattern How the Composite Pattern lets you treat a group of things the same way you'd treat a single thing in Python.
- Python Facade Pattern How the Facade Pattern gives you one simple button instead of a confusing control panel in Python.