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 separateProxyobject that acts as placeholder for another object (Subject). A proxy implements theSubjectinterface so that it can act as placeholder anywhere a subject is expected. -
Work through a
Proxyobject to control the access to an (already existing) object.
-
Provide a surrogate or
placeholder for another object to control
access to it.
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
Proxyobject that performs additional functionality when accessing an (already existing) object.
A proxy implements theSubjectinterface 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
Proxyobject:-
Define a class (
Proxy) that implements arbitrary functionality to control the access to aRealSubjectobject. -
"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.
-
Define a class (
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.
-
A protection proxy acts as placeholder for
sensitive objects
Structure
Static Class Structure
-
Client-
Refers to the
Subjectinterface.
-
Refers to the
-
Subject-
Defines a common interface
for
RealSubjectandProxyobjects.
-
Defines a common interface
for
-
RealSubject- Defines objects that get substituted.
-
Proxy-
Maintains a reference
to a
RealSubjectobject (realSubject). -
Implements additional functionality
to control the access
to this
RealSubjectobject. -
Implements the
Subjectinterface so that it can act as substitute whenever aSubjectobject is expected.
-
Maintains a reference
to a
Dynamic Object Collaboration
-
In this sample scenario,
a
Clientobject works through aProxyobject that controls the access to aRealSubjectobject. -
The
Clientobject callsoperation()on theProxyobject. -
The
Proxymay perform additional functionality and forwards the request to theRealSubjectobject, 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.