Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Py

Python

Readable, batteries-included, and the default language of AI.

Paradigm
Multi-paradigm, OOP, functional
Typing
Dynamic, strong, optional type hints
Memory
Reference counting + cycle GC
Runs
CPython bytecode interpreter
Concurrency
asyncio, threads (GIL), multiprocessing
Structural pattern

Proxy

Intent

Provides a substitute that controls access to another object.

Problem

A heavy database object should only be created when actually needed, and access should be logged.

Solution

A proxy with the same interface creates, caches, checks or logs, then forwards to the real object.

Real-world analogy

Think of it like this

A credit card stands in for your bank account, which stands in for cash. The shop accepts it the same way.

Structure

association / extends implements / depends on
Client 4 «interface» ServiceInterface + operation() 1 Proxy - realService: Service+ checkAccess()+ operation() 3 Service + operation() 2

Hover or tap a numbered role to highlight it in the diagram.

  1. 1ServiceInterface — Service Interface is shared by the service and the proxy.
  2. 2Service — Service contains the real business logic.
  3. 3Proxy — Proxy holds a reference to the service. It adds lazy loading, caching, logging or access control, then delegates.
  4. 4Client — Client uses the service and the proxy through the same interface.

Code example

A Python code sample for Proxy is coming soon. Factory Method, Singleton, Decorator, Observer and Strategy already have code in all six languages.

Applicability

Use Proxy when lazy initialisation (virtual proxy).

Use Proxy when access control.

Use Proxy when remote services.

Use Proxy when logging and caching.

Pros and cons

Pros

  • Control the service without clients knowing
  • Works even if the service isn't ready

Cons

  • Possible response delay
  • More classes

Relations with other patterns

Check yourself

Check yourself

Add caching and access checks in front of a slow remote image service without changing its clients.

Saved in this browser only. Part of Structural patterns (0/7).