Proxy Pattern
Control creation or access through a substitute that exposes the same interface as the real object.
· Last updated: 2026-11-07 · 10 min read
Overview
When a gallery screen postpones heavy image loading until the image is actually needed, putting a “loaded yet?” check in each screen copies the same test into every call site. Rather than checking whether a load is needed at every call site, you can put a stand-in in place that is used exactly like an image and takes over the timing of the load.
A proxy stands in for the real object, receives requests through the same interface, and governs when a request reaches that object. What the stand-in owns is a single decision: when to hand the request over. The example below is a lazy-creation proxy that creates the real object on the first draw.
UML
RealImage and ImageProxy both implement IImage, and because ImageProxy holds a Lazy<RealImage>, it hands the request to the real object only when Draw() is called. One shared contract means callers never know which class they hold.
History
GoF cataloged Proxy as a structural pattern in Design Patterns in 1994. A substitute or placeholder controls access to another object.1
GoF grouped proxies by how they control access: a remote proxy hides the fact that an object lives in another address space, a virtual proxy creates an expensive object on demand, a protection proxy checks caller permissions, and a smart reference adds housekeeping such as reference counting or locking on access. Where a Decorator wraps a component supplied by the caller to layer on behavior, a Proxy governs access to its subject and often manages the subject's creation itself.
Standing a local representative in front of a remote or non-resident resource goes back to Birrell and Nelson's 1984 RPC stubs and Marc Shapiro's 1986 paper that coined the Proxy Principle for distributed object systems.2 Equally foundational is the 1994 warning by Waldo, Wyant, Wollrath, and Kendall in "A Note on Distributed Computing": even if a remote proxy shares the exact type signature of an in-memory object, latency, partial failure, and concurrency semantics remain fundamentally different.3
Why use it?
From the caller's side, the real image and the proxy are handled in the same way. Choices such as whether to defer loading or under which conditions access is permitted, however, all come together in the proxy. The caller only draws; the stand-in holds the timing of the expensive work.
An authorization proxy must check current permission on each request. The name suggests a proxy handles anything on your behalf, but this example covers deferred creation only. It supplies neither authentication nor remote communication.
Example
Compare the instance count right after building the proxy with the count after the first call to Draw(). Counting constructions in memory rather than opening files shows the exact moment creation is deferred.
using System;
using System.Collections.Generic;
using System.Linq;
IImage image = new ImageProxy();
Console.WriteLine(RealImage.Created);
Console.WriteLine(image.Draw());
Console.WriteLine(RealImage.Created);
// Let callers use the real subject and its proxy through the same contract.
interface IImage { string Draw(); }
sealed class RealImage : IImage
{
public static int Created { get; private set; }
public RealImage() => Created++;
public string Draw() => "draw image";
}
// Control construction time and delegate the actual work to the created object.
sealed class ImageProxy : IImage
{
private readonly Lazy<RealImage> image = new(() => new RealImage());
public string Draw() => image.Value.Draw();
}
0
draw image
1Creating the proxy does not bring RealImage into existence; one instance appears only after Draw() touches Lazy.Value. Here the code shows the creation point moving.
An extended example
Drawing twice through the same proxy still creates the real object only once.
using System;
using System.Collections.Generic;
using System.Linq;
IImage image = new ImageProxy();
Console.WriteLine(image.Draw());
Console.WriteLine(image.Draw());
Console.WriteLine("created: " + RealImage.Created);
// Let callers use the real subject and its proxy through the same contract.
interface IImage { string Draw(); }
sealed class RealImage : IImage
{
public static int Created { get; private set; }
public RealImage() => Created++;
public string Draw() => "draw image";
}
// Control construction time and delegate the actual work to the created object.
sealed class ImageProxy : IImage
{
private readonly Lazy<RealImage> image = new(() => new RealImage());
public string Draw() => image.Value.Draw();
}
draw image
draw image
created: 1Lazy reuses the real object rather than the result of Draw(), and because Draw() on that object runs on every call, reusing it is not the same as caching result data. Construction count and result cache are two separate things. If the real resource is a file or a GPU object, you also need to settle who releases it and when.
TypeScript example
The division is the same as in the earlier example. Callers that request a draw know only IImage, while the proxy decides when the real object is created. Separating access control from the actual drawing is what keeps each screen from repeating the deferred-creation condition.
// Let callers use the real subject and its proxy through the same contract.
interface IImage { draw(): string; }
class RealImage implements IImage {
static created = 0;
constructor() { RealImage.created++; }
draw(): string { return "draw image"; }
}
// Control construction time and delegate the actual work to the created object.
class ImageProxy implements IImage {
#image: RealImage | undefined;
draw(): string {
this.#image ??= new RealImage();
return this.#image.draw();
}
}
const image: IImage = new ImageProxy();
console.log(RealImage.created);
console.log(image.draw());
console.log(image.draw());
console.log("created: " + RealImage.created);0
draw image
draw image
created: 1The #image field expresses the deferred creation performed by C# Lazy<RealImage>. Same role, held differently. It reuses the object rather than caching draw() results, and reads no image files.
When it becomes an anti-pattern
A proxy that hides file access, network calls or retries changes latency and failure behavior behind a familiar method. The difference never shows up in the method name. Inserting remote work while callers still expect a local calculation makes the apparent transparency conceal failures. Real I/O needs explicit cancellation, failure and execution-count contracts.
The same trap appears in ORM virtual proxies (such as lazy-loaded navigation properties in Entity Framework or Hibernate): touching a proxy property after the underlying database context has closed throws a lifecycle exception, and iterating over a list of entities while touching a lazy proxy property fires one hidden query per row—the classic N+1 query problem (see Data pagination). Whenever a proxy crosses an I/O or network boundary, pair it with explicit Timeout Pattern, Circuit Breaker Pattern, and Result Type contracts rather than pretending the call cannot fail.
Combining access checks and lazy creation with Proxy<T>
ISubject<T> and Proxy<T> both return T from Request(). Combining a protection check with lazy creation ensures that an unauthorized call is rejected before the factory ever constructs the underlying subject.
In the example below, calling Request() while canAccess is false returns "denied" and leaves created at 0. Once access is granted, the first permitted call runs the factory to build RealSubject<string>, and subsequent permitted calls reuse that same instance.
C#
using System;
bool canAccess = false;
int created = 0;
ISubject<string> report = new Proxy<string>(
() => canAccess,
() => { created++; return new RealSubject<string>("report"); },
"denied");
Console.WriteLine(report.Request());
Console.WriteLine(created);
canAccess = true;
Console.WriteLine(report.Request());
Console.WriteLine(report.Request());
Console.WriteLine(created);
interface ISubject<T> { T Request(); }
sealed class RealSubject<T>(T Value) : ISubject<T>
{
public T Request() => Value;
}
sealed class Proxy<T> : ISubject<T>
{
private readonly Func<bool> canAccess;
private readonly Lazy<ISubject<T>> subject;
private readonly T deniedValue;
public Proxy(Func<bool> canAccess, Func<ISubject<T>> factory, T deniedValue)
{
this.canAccess = canAccess;
subject = new Lazy<ISubject<T>>(factory);
this.deniedValue = deniedValue;
}
// Forward only permitted requests to the lazily created subject.
public T Request() => canAccess() ? subject.Value.Request() : deniedValue;
}denied
0
report
report
1TypeScript
interface Subject<T> { request(): T; }
class RealSubject<T> implements Subject<T> {
readonly #value: T;
constructor(value: T) { this.#value = value; }
request(): T { return this.#value; }
}
class ProxySubject<T> implements Subject<T> {
#subject: Subject<T> | undefined;
readonly #canAccess: () => boolean;
readonly #factory: () => Subject<T>;
readonly #deniedValue: T;
constructor(canAccess: () => boolean, factory: () => Subject<T>, deniedValue: T) {
this.#canAccess = canAccess;
this.#factory = factory;
this.#deniedValue = deniedValue;
}
// Forward only permitted requests to the lazily created subject.
request(): T {
if (!this.#canAccess()) return this.#deniedValue;
this.#subject ??= this.#factory();
return this.#subject.request();
}
}
let canAccess = false;
let created = 0;
const report: Subject<string> = new ProxySubject(
() => canAccess,
() => { created++; return new RealSubject("report"); },
"denied",
);
console.log(report.request());
console.log(created);
canAccess = true;
console.log(report.request());
console.log(report.request());
console.log(created);denied
0
report
report
1Because canAccess runs on every call even after the subject exists, revoking permission later immediately stops delegation to the cached instance as well.
Related reading
Footnotes
- Gamma, Helm, Johnson, Vlissides. Design Patterns (1994). Publisher record. Structural patterns, Proxy. ↩
- Marc Shapiro, "Structure and Encapsulation in Distributed Systems: The Proxy Principle," Proceedings of the 6th International Conference on Distributed Computing Systems (ICDCS), IEEE, 1986, pp. 198–204. ↩
- Jim Waldo, Geoff Wyant, Ann Wollrath, and Sam Kendall, "A Note on Distributed Computing," Sun Microsystems Laboratories Technical Report SMLI TR-94-29, November 1994. ↩