JavaOmnibus
GoF Proxy
GoF pattern • Structural
GoFStructuralJava example

Proxy solves a specific structural problem in the design.

Stand in for another object to control access or add indirection.

Pattern overview

GoF pattern

Stand in for another object to control access or add indirection.

Intent

Provide a surrogate or placeholder for another object to control access, loading, or behavior.

Problem

Direct access to an object is expensive, remote, sensitive, or needs additional control.

Solution

Expose the same interface through a proxy that decides when and how to delegate to the real object.

Fit and tradeoffs

FamilyGoF
CategoryStructural
Best used when
  • For remote calls, lazy loading, access control, or caching.
  • When clients should not know whether the real object is local or remote.
Tradeoffs
  • Adds hidden indirection and potential latency surprises.
  • Can blur the line between cheap local calls and expensive remote ones.

Java example

This example is intentionally compact so the pattern shape stays easy to see.

interface ReportRepository {
    Report findById(String id);
}

final class CachingReportRepository implements ReportRepository {
    private final ReportRepository delegate;
    private final Map<String, Report> cache = new HashMap<>();

    CachingReportRepository(ReportRepository delegate) {
        this.delegate = delegate;
    }

    public Report findById(String id) {
        return cache.computeIfAbsent(id, delegate::findById);
    }
}

Related pages

Keep exploring