GofPatterns Gofpatterns

Structural Patterns  «Prev  Next»

Proxy Pattern

The Proxy pattern provides a surrogate or placeholder object to control access to the original object.
There are several types of implementations of the Proxy pattern with the
  1. Remote proxy and
  2. Virtual proxy
  3. being the most common.
The figure below illustrates the Proxy pattern.

The following image detials the proxy pattern.

Proxy Pattern Code

Problem Solution using the Proxy Pattern:

You are currently designing your own Desktop Publishing application, as you have not found any that do exactly what you want with existing applications. As part of the design you are using a Controller to which you send all GUI requests. Not all objects can process the same commands. For example you cannot select the spell check tool when an image has the focus. To stop any possible errors you would like to filter out some of the messages as they are passed from these objects to the Controller object. What pattern could you use? In this scenario what you are essentially trying to do is filter all packets that don't meet a certain set of requirements. This behavior is just like a Proxy server dropping packets from certain IP address etc.
  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.
When to Use:
You should use the proxy pattern when:
You need a more versatile or sophisticated reference to an object than a simple pointer.