# Proxy pattern

Provide a surrogate or placeholder for another object to control access to it.

A Proxy can also be defined as a surrogate. In the real world, a cheque or credit card is a proxy for what is in our bank account. It can be used in place of cash, which is what is needed, and provides a means of accessing that cash when required. And that's exactly what the Proxy pattern does - controls and manage access to the object they are "protecting".

The Proxy is known as a structural pattern, as it's used to form large object structures across many disparate objects. The definition of Proxy provided in the original Gang of Four book on DesignPatterns states:

*Allows for object level access control by acting as a pass through entity or a placeholder object.*

![](https://3684907566-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgDoNtvxXMr-cH2wym1%2F-LgDoT90hWHh6U09F42g%2F-LgDobcm9vUhH62jGlGd%2Fproxy.png?generation=1559321803948425\&alt=media)

![](https://3684907566-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgDoNtvxXMr-cH2wym1%2F-LgDoT90hWHh6U09F42g%2F-LgDobcoAfw_xQ0KPMcJ%2Fproxy_sequence_diagram.png?generation=1559321841284228\&alt=media)

## Proxy design pattern:

1. Create a "wrapper" for a remote, or expensive, or sensitive target
2. Encapsulate the complexity/overhead of the target in the wrapper
3. The client deals with the wrapper
4. The wrapper delegates to the target
5. To support plug-compatibility of wrapper and target, create an interface

## Would I use this pattern?

* The object being represented is external to the system
* Objects need to be created on demand
* Access control for the original object is required
* Added functionality is required when an object is accessed.
