## Proxy pattern

### Introduction

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

### Participants:

#### Proxy

- 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.
- provides an interface identical to Subject's so that a proxy can be substituted for the real subject.
- controls access to the real subject and may be responsible for creating and deleting it.
- other responsibilities depend on the kind of proxy:
  - 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.
  - virtual proxies may cache additional information about the real subject so they can postpone accessing it.
  - protection proxies check that the caller has the access permission required to perform a request.
- this participant can not be read-only.
- this participant can not be an interface.

#### Subject

- defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
- this participant can not be read-only.

#### RealSubject

- defines the real object that the proxy represents.
- this participant can not be read-only.

In the "Attribute" field you can specify the name of an attribute where the Proxy will keep a reference to the Real Subject object it represents.

The "Initialization variant" option defines the kind of initialization of that attribute.

The "Copy documentation" option controls whether to copy JavaDoc comments from methods in interfaces participating in the pattern to the stubs of these methods made by the pattern in classes implementing such interfaces.

The "Create pattern links" option controls whether to generate additional links which can be used by this pattern later to determine classes and interfaces participating in the pattern. That means that if you selected this option and used the pattern to create a set of classes and interfaces, the pattern invoked for some participant later (via the popup menu item "Choose Pattern...") will automatically find (if it is possible) all other participants and fill in participant fields with their names.

Also, if you applied the pattern with this option on and invoked the pattern via the popup menu item "Choose Pattern..." for some participant later, the pattern will have additional field called "Use selected class as", containing possible roles for the selected element.

This option is very useful when you are planning to change something in the classes/interfaces participating in the pattern. For example, if this option is on and after creation of the classes and interfaces you added several methods to certain interface-participant (and this change must be reflected somehow in other participants), all you have to do is to select this changed interface, invoke the "Choose Pattern..." dialog for this element and select the original pattern. After that the pattern will determine other participants and you will only have to press "Finish". The pattern will modify all other classes and interfaces accordingly to your changes.

### Applicability

Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer.