Design Patterns

Proxy



Design Pattern Type: Structural

GoF Statement of Intent:


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



A proxy is a class acting as an interface to something else. It may interface to a network connection, a large object in memory, a file, or some other resource that is expensive (a resource hog) or impossible to duplicate.
UML Diagram:

Discussion and In-class Example:

In the Proxy Design Pattern we extended the example of the GumballMachine application to add the ability to remotely monitor gumball machines. But having to use the C++ Remote Method Invocation (RMI) interface can be expensive in time and memory. A solution to this problem is the Proxy.

Using the Proxy both the local GumballMonitor and the remote GumballMachine each have a Proxy object which stands in for them. The GumballMonitor thinks it is communicating with the remote GumballMachine when in fact it is communicating with a Proxy. The same is true for the GumballMachine.

Proxies are especially useful when it might be difficult due to constraints of time, memory, or band width to create a needed object. The proxy can stand in for the real object while it is being created in the background. Proxies are also useful to provide security for certain objects. The proxy can stand between the object and other objects wanting to communicate with it and provide protection against dangerous or unauthorized access.