Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Proxy pattern

Заместитель

A structural design pattern that puts a stand-in in front of another object. The stand-in offers the same interface, but controls access to the underlying object—for example, by delaying its creation until it is needed.

What the proxy pattern does

A proxy stands in for an object that the caller would otherwise use directly. Suppose a report viewer calls render(). It doesn't need to know whether the report is already in memory or still needs to be read from disk. A proxy can handle that decision without putting file-loading logic in the viewer.

How the pieces fit

The caller depends on a shared interface: the operations it can call and what those operations promise. The real object performs the core work. The proxy exposes the same operations but decides what to do with each call. It might forward the call immediately, create the target first, return a cached result, or reject the call if the caller lacks permission. In a language such as Python, the shared interface doesn't have to be a declared type; both objects just need to support the expected operations.

Example: load a report only when it's needed

from pathlib import Path

class Report:
    def __init__(self, path):
        self.text = Path(path).read_text(encoding='utf-8')

    def render(self):
        return self.text


class LazyReport:
    def __init__(self, path):
        self.path = path
        self._report = None

    def render(self):
        if self._report is None:
            self._report = Report(self.path)
        return self._report.render()

The viewer calls render() in either case. Creating a LazyReport does no file I/O; the first call reads the file, and subsequent calls reuse the same Report. The trade-off is that both the cost of reading the file and any read error move to the first call. This example assumes sequential use: if threads share the proxy, the check alone doesn't guarantee that the report is created only once.

Where proxies show up

  • A virtual proxy delays expensive work. An ORM, for example, may wait until code accesses a related entity before fetching it from the database.
  • A protection proxy checks permissions before forwarding a call. If callers can reach the protected operation directly, that check alone isn't an access-control boundary.
  • A remote proxy represents an object or service in another process. Calling a method may look local, but the caller still has to account for network latency and timeouts.

How it comes up in interviews

A common question is how to tell a Proxy from a Decorator when both wrap an object and expose the same interface. Explain the intent: a Proxy manages access, while a Decorator adds behavior or responsibilities. An Adapter, by contrast, presents a different interface so incompatible components can work together. If asked to implement a lazy proxy, be ready to show which call creates the target, what happens if creation fails, and whether concurrent callers can initialize it more than once.