In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, the proxy pattern is a
software design pattern
In software engineering, a software design pattern or design pattern is a general, reusable solution to a commonly occurring problem in many contexts in software design. A design pattern is not a rigid structure to be transplanted directly into s ...
. A ''proxy'', in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be
forwarding to the real object, or can provide additional logic. In the proxy, extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.
Overview
The Proxy
design pattern is one of the twenty-three well-known
''
GoF design patterns''
that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
What problems can the Proxy design pattern solve?
Source:
* The access to an object should be controlled.
* Additional functionality should be provided when accessing an object.
When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights.
What solution does the Proxy design pattern describe?
Define a separate
Proxy
object that
* can be used as a substitute for another object (
Subject
), and
* implements additional functionality to control the access to this subject.
This makes it possible to work through a
Proxy
object to perform additional functionality when accessing a subject, like checking the access rights of clients accessing a sensitive object.
To act as a substitute for a subject, a proxy must implement the
Subject
interface. Clients can't tell whether they work with a subject or its proxy.
See also the UML class and sequence diagram below.
Structure
UML class and sequence diagram
In the above
UML class diagram
In software engineering,
a class diagram
in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the re ...
,
the
Proxy
class implements the
Subject
interface so that it can act as substitute for
Subject
objects. It maintains a reference (
realSubject
)
to the substituted object (
RealSubject
) so that it can forward requests to it
(
realSubject.operation()
).
The sequence diagram
shows the run-time interactions: The
Client
object
works through a
Proxy
object that
controls the access to a
RealSubject
object.
In this example, the
Proxy
forwards the request to the
RealSubject
, which performs the request.
Class diagram
Possible usage scenarios
Remote proxy
In
distributed object communication, a local object represents a remote object (one that belongs to a different address space). The local object is a proxy for the remote object, and method invocation on the local object results in
remote method invocation In a distributed computing environment, distributed object communication realizes communication between distributed objects. The main role is to allow objects to access data and invoke methods on remote objects (objects residing in non-local memory ...
on the remote object. An example would be an
ATM implementation, where the ATM might hold proxy objects for bank information that exists in the remote server.
Virtual proxy
In place of a complex or heavy object, a skeleton representation may be advantageous in some cases. When an underlying image is huge in size, it may be represented using a virtual proxy object, loading the real object on demand.
Protection proxy
A protection proxy might be used to control access to a resource based on access rights.
See also
*
Composite pattern
In software engineering, the composite pattern is a partitioning design pattern (computer science), design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. Th ...
*
Decorator pattern
In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other instances of the same class. The decorator pattern is often ...
*
Lazy initialization In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specificall ...
References
External links
*
PerfectJPattern Open Source Project Provides componentized implementation of the Proxy Pattern in Java
*
Proxy Design Pattern*
Proxy pattern description from the Portland Pattern Repository
{{Design Patterns Patterns
Software design patterns
Articles with example C Sharp code
Articles with example Java code