Pattern: Proxy

Synopsis

The proxy pattern implements the interface of a class in order to provide indirect access to it. This allows the proxy to act as an intermediary and control access to the target class. A common variant of the Proxy pattern is the Remote Proxy, which provides access to a remote service.

Context

Suppose you have a class that you would like to provide controlled or scheduled access to. Or perhaps you have a remote service that you would like to provide access to, but would like to provide a local failover for. The Proxy pattern can serve as an intermediary or substiture to make the interface for this class available, while providing a degree of control while enhancing or replacing certain functionality.

Forces

In the typical case, we have a target class whose interface we would like to provide reliable, controlled access to. The client uses the interface to the proxy as if it were the interface to the target class, therefore the proxy should implement the same interface as the target class.

Solution

We create a class that implements the the service provider class's interface, and keeps a reference to the service provider class. Similar to the Adapter, the proxy recieves the client's calls and either forwards them to the server, or deals with them in its own fashion.

The general pattern for the Proxy pattern is as follows:

Consequences

Implementation

A code example for a proxy is in this zip file.

Related Patterns

Remote Proxy
Virtual Proxy
Cache Proxy
Firewall Proxy


Modified: 28-Jan-2004
Paolo Piselli