In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, the proxy pattern is a
software design pattern
In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code ...
. 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?
* 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 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. For example, to check the access rights of clients accessing a sensitive object.
To act as 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
The Unified Modeling Language (UML) is a general-purpose, developmental modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system.
The creation of UML was originally m ...
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 rela ...
,
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 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 memor ...
, 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.
Example
C#
interface ICar
// Real Object
public class Car : ICar
// Proxy Object
public class ProxyCar : ICar
public class Driver
// How to use above Proxy class?
private void btnProxy_Click(object sender, EventArgs e)
Output
Sorry, the driver is too young to drive.
Car has been driven!
Notes:
* A proxy may hide information about the real object to the client.
* A proxy may perform optimization like on demand loading.
* A proxy may do additional house-keeping job like audit tasks.
* Proxy design pattern is also known as surrogate design pattern.
C++
#include
#include
class ICar ;
class Car : public ICar ;
class ProxyCar : public ICar ;
int main()
Crystal
abstract class AbstractCar
abstract def drive
end
class Car < AbstractCar
def drive
puts "Car has been driven!"
end
end
class Driver
getter age : Int32
def initialize(@age)
end
end
class ProxyCar < AbstractCar
private getter driver : Driver
private getter real_car : AbstractCar
def initialize(@driver)
@real_car = Car.new
end
def drive
if driver.age <= 16
puts "Sorry, the driver is too young to drive."
else
@real_car.drive
end
end
end
# Program
driver = Driver.new(16)
car = ProxyCar.new(driver)
car.drive
driver = Driver.new(25)
car = ProxyCar.new(driver)
car.drive
Output
Sorry, the driver is too young to drive.
Car has been driven!
Delphi / Object Pascal
// Proxy Design pattern
unit DesignPattern.Proxy;
interface
type
// Car Interface
ICar = interface
procedure DriveCar;
end;
// TCar class, implementing ICar
TCar = Class(TInterfacedObject, ICar)
class function New: ICar;
procedure DriveCar;
End;
// Driver Interface
IDriver = interface
function Age: Integer;
end;
// TDriver Class, implementing IDriver
TDriver = Class(TInterfacedObject, IDriver)
private
FAge: Integer;
public
constructor Create(Age: Integer); Overload;
class function New(Age: Integer): IDriver;
function Age: Integer;
End;
// Proxy Object
TProxyCar = Class(TInterfacedObject, ICar)
private
FDriver: IDriver;
FRealCar: ICar;
public
constructor Create(Driver: IDriver); Overload;
class function New(Driver: IDriver): ICar;
procedure DriveCar;
End;
implementation
class function TCar.New: ICar;
begin
Result := Create;
end;
procedure TCar.DriveCar;
begin
WriteLn('Car has been driven!');
end;
constructor TDriver.Create(Age: Integer);
begin
inherited Create;
FAge := Age;
end;
class function TDriver.New(Age: Integer): IDriver;
begin
Result := Create(Age);
end;
function TDriver.Age: Integer;
begin
Result := FAge;
end;
constructor TProxyCar.Create(Driver: IDriver);
begin
inherited Create;
Self.FDriver := Driver;
Self.FRealCar := TCar.Create AS ICar;
end;
class function TProxyCar.New(Driver: IDriver): ICar;
begin
Result := Create(Driver);
end;
procedure TProxyCar.DriveCar;
begin
if (FDriver.Age <= 16)
then WriteLn('Sorry, the driver is too young to drive.')
else FRealCar.DriveCar();
end;
end.
Usage
program Project1;
uses
DesignPattern.Proxy in 'DesignPattern.Proxy.pas';
begin
TProxyCar.New(TDriver.New(16)).DriveCar;
TProxyCar.New(TDriver.New(25)).DriveCar;
end.
Output
Sorry, the driver is too young to drive.
Car has been driven!
Java
The following
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
example illustrates the "virtual proxy" pattern. The
ProxyImage
class is used to access a remote method.
The example creates first an interface against which the pattern creates the classes. This interface contains only one method to display the image, called
displayImage()
, that has to be coded by all classes implementing it.
The proxy class
ProxyImage
is running on another system than the real image class itself and can represent the real image
RealImage
over there. The image information is accessed from the disk. Using the proxy pattern, the code of the
ProxyImage
avoids multiple loading of the image, accessing it from the other system in a memory-saving manner. The lazy loading demonstrated in this example is not part of the proxy pattern, but is merely an advantage made possible by the use of the proxy.
interface Image
// On System A
class RealImage implements Image
// On System B
class ProxyImage implements Image
class ProxyExample
Output
Loading HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Loading HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo1
JavaScript
// Driver class
class Driver
// Car class
class Car
// Proxy car class
class ProxyCar
// Run program
const driver = new Driver(16)
const car = new ProxyCar(driver)
car.drive()
const driver2 = new Driver(25)
const car2 = new ProxyCar(driver2)
car2.drive()
Output
Sorry, the driver is too young to drive.
Car has been driven!
More advanced proxies involve the
Proxy
object, which can intercept and redefine fundamental operations such as accessing properties. The handler functions in this case are sometimes called ''traps''.
PHP
displayImage(); // Loading necessary
$image1->displayImage(); // Loading unnecessary
$image2->displayImage(); // Loading necessary
$image2->displayImage(); // Loading unnecessary
$image1->displayImage(); // Loading unnecessary
Output
Loading HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Loading HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo1
Python
"""
Proxy pattern example.
"""
from abc import ABCMeta, abstractmethod
NOT_IMPLEMENTED = "You should implement this."
class AbstractCar:
__metaclass__ = ABCMeta
@abstractmethod
def drive(self):
raise NotImplementedError(NOT_IMPLEMENTED)
class Car(AbstractCar):
def drive(self) -> None:
print("Car has been driven!")
class Driver:
def __init__(self, age: int) -> None:
self.age = age
class ProxyCar(AbstractCar):
def __init__(self, driver) -> None:
self.car = Car()
self.driver = driver
def drive(self) -> None:
if self.driver.age <= 16:
print("Sorry, the driver is too young to drive.")
else:
self.car.drive()
driver = Driver(16)
car = ProxyCar(driver)
car.drive()
driver = Driver(25)
car = ProxyCar(driver)
car.drive()
Output
Sorry, the driver is too young to drive.
Car has been driven!
Rust
trait ICar
struct Car
impl ICar for Car
impl Car
struct ProxyCar<'a>
impl<'a> ICar for ProxyCar<'a>
impl<'a> ProxyCar<'a>
# fg(test)mod tests
Output
Sorry, the car is to young for you to drive.
Car has been driven!
See also
*
Composite pattern In software engineering, the composite pattern is a partitioning 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. The intent of a composite is to "compo ...
*
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 objects from 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