The 23 GoF design patterns and Dependency Injection – choose a pattern Abstract Factory design patternBuilder design patternFactory Method design patternPrototype design patternSingleton design patternAdapter design patternBridge design patternComposite design patternDecorator design patternFacade design patternFlyweight design patternProxy design patternChain of Responsibility design patternCommand design patternInterpreter design patternIterator design patternMediator design patternMemento design patternObserver design patternState design patternStrategy design patternTemplate Method design patternVisitor design patternDependency Injection design pattern

Proxy Design Pattern

Structural pattern. Structural design patterns are concerned with composing classes and objects into larger structures.

Intent: "Provide a surrogate or placeholder for another object to control access to it." [GoF]

Open in the interactive referenceDownload the PDF reference

Intent

The intent of the Proxy design pattern is to:
"Provide a surrogate or placeholder for another object
to control access to it."
[GoF]
See Problem and Solution sections for a more structured description of the intent.

  • The Proxy design pattern solves problems like:
    • How can the access to an object be controlled?
    • How can additional functionality be provided when accessing an object?
  • For example, the access to sensitive, expensive, or remote objects should be controlled.
  • The Proxy pattern describes how to solve such problems:
    • Provide a surrogate or placeholder for another object to control access to it.
      Define a separate Proxy object that acts as placeholder for another object (Subject). A proxy implements the Subject interface so that it can act as placeholder anywhere a subject is expected.
    • Work through a Proxy object to control the access to an (already existing) object.

Problem

The Proxy design pattern solves problems like:
How can the access to an object be controlled?
How can additional functionality be provided when accessing an object?
See Applicability section for all problems Proxy can solve. See Solution section for how Proxy solves the problems.

  • Often it should be possible to provide additional functionality when accessing an (already existing) object.
    "[…] whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer." [GoF, p208]
  • For example, when accessing sensitive objects, it should be possible to check that clients have the required access rights.
  • For example, when accessing expensive objects, it should be possible to create them on demand (i.e., to defer their instantiation until they are actually needed) and cache their data.
  • For example, when accessing remote objects, it should be possible to hide complex network communication details from clients.

Solution

The Proxy design pattern provides a solution:
Define a separate Proxy object that acts as substitute for another object (Subject).
Work through a Proxy object to control the access to a real subject.
Describing the Proxy design in more detail is the theme of the following sections.
See Applicability section for all problems Proxy can solve.

  • The key idea in this pattern is to work through a separate Proxy object that performs additional functionality when accessing an (already existing) object.
    A proxy implements the Subject interface so that it can act as substitute wherever a subject is expected. Clients do not know whether they are working with a real subject or its proxy.
  • Define a separate Proxy object:
    • Define a class (Proxy) that implements arbitrary functionality to control the access to a RealSubject object.
    • "The Proxy pattern introduces a level of indirection when accessing an object. The additional indirection has many uses, depending on the kind of proxy:" [GoF, p210] For example:
      A protection proxy acts as placeholder for sensitive objects
      to check that clients have the required access rights.
      A virtual proxy acts as placeholder for expensive objects
      to defer their creation until they are actually needed.
      A remote proxy acts as placeholder for remote objects
      to hide complex network communication details from clients.

Applicability

Design Problems

  • Controlling Access to Objects
    • How can the access to an object be controlled?
    • How can additional functionality be provided when accessing an object?
  • Common Kinds of Proxies
    • A protection proxy acts as placeholder for sensitive objects
      to check that clients have the required access rights.
    • A virtual proxy acts as placeholder for expensive objects
      to defer their creation until they are actually needed.
    • A remote proxy acts as placeholder for remote objects
      to hide complex network communication details from clients.

Structure

Static Class Structure

  • Client
    • Refers to the Subject interface.
  • Subject
    • Defines a common interface for RealSubject and Proxy objects.
  • RealSubject
    • Defines objects that get substituted.
  • Proxy
    • Maintains a reference to a RealSubject object (realSubject).
    • Implements additional functionality to control the access to this RealSubject object.
    • Implements the Subject interface so that it can act as substitute whenever a Subject object is expected.

Dynamic Object Collaboration

  • In this sample scenario, a Client object works through a Proxy object that controls the access to a RealSubject object.
  • The Client object calls operation() on the Proxy object.
  • The Proxy may perform additional functionality and forwards the request to the RealSubject object, which performs the request.
  • See also Sample Code / Example 1.

Not included on this page: Motivation, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.