Proxy Pattern

# Proxy

- Purpose
- Structure
- Applications
- Consequences

## Purpose

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

## Structure

- Proxy
  1. maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
  2. provides an interface identical to Subject's so that a proxy can by substituted for the real subject.
  3. controls access to the real subject and may be responsible for creating and deleting it.
  4. other responsibilities depend on the kind of proxy:
  5. remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
  6. virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real image's extent.
  7. protection proxies check that the caller has the access permissions required to perform a request.
- Subject : defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
- RealSubject : defines the real object that the proxy represents.

## Applications

- A remote proxy provides a local representative for an object in a different address space.
- A virtual proxy creates expensive objects on demand. The ImageProxy described in the Motivation is an example of such a proxy.
- A protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.

## Consequences

- The Proxy pattern introduces a level of indirection when accessing an object. The additional indirection has many uses, depending on the kind of proxy:
  1. A remote proxy can hide the fact that an object resides in a different address space.
  2. A virtual proxy can perform optimizations such as creating an object on demand.
  3. Both protection proxies and smart references allow additional housekeeping tasks when an object is accessed.