AlgoViz

Proxy pattern

medium 50 min

A proxy stands in for another object, with the same interface, and controls access to it: it creates it late, checks the caller, remembers results, or reaches it over a network.

Watch lesson4:53

Where it shows up

The same pattern appears across everyday software.

  • Hibernate

    Lazy relations are represented by proxies that are initialized at first use. Using one after the session is closed raises LazyInitializationException.

  • Spring AOP

    Uses JDK dynamic proxies or CGLIB subclasses. The reference explains that self-invocation does not run the advice, because the call does not go through the proxy.

  • JavaScript Proxy

    `new Proxy(target, handler)` intercepts fundamental operations such as getting and setting properties through traps.

  • Vue

    Vue 3 makes reactive objects with JavaScript Proxies, so that reads and writes of properties can be tracked.

  • Java dynamic proxies

    `java.lang.reflect.Proxy` creates instances that implement given interfaces and send every call to an InvocationHandler.

  • Python weakref

    `weakref.proxy` returns a proxy that uses a weak reference. Using it after the object is gone raises ReferenceError.

  • gRPC

    On the client there is a local object, the stub (called client in some languages), with the same methods as the service. It sends the request and returns the server's response.

  • nginx

    The proxy module passes requests to another server with `proxy_pass` and can keep responses with `proxy_cache`: the idea of the pattern between machines.

  • Amazon API Gateway

    Acts as a front door for applications to access data or functionality from backend services, with authorization, throttling and monitoring before them.