Applied Design Patterns with Java

Structural :: Proxy (207) {C ch 15}

Applicability

The Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several situations in which the Proxy pattern is applicable:

  1. A remote Proxy provides a local representative for an object in a different address space;
  2. A virtual Proxy creates expensive objects on demand. The ImageProxy described in the Motivation is an example of such a proxy;
  3. A protection Proxy controls access to the original object. Protection proxies are useful when objects should have different access rights;
  4. A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. Typical uses include: counting the number of references to the real object so that it can be freed automatically when there are no more references (also called smart pointers); loading a persistent object into memory when it's first referenced; and checking that the real object is locked before it's accessed to ensure that no other object can change it.

Structure

The Proxy pattern has this general organization:

Here's a possible object diagram of a Proxy structure at run-time:


Participants

Catalog Structural Prev Next