Proxy pattern

The proxy, also called deputy, is a design pattern in the field of software development and belongs to the category of structural patterns (English structural design patterns ). The pattern is used to shift the control over an object on an upstream proxy object. It is a design pattern of the so-called Gang of Four.

A proxy in its most general form, is a class that acts as an interface to another "subject". This subject may be, for example, a network connection, a large object in the memory, file, or other resource. As a representative of this subject the proxy can control the production of the subject as well as access to it.

Use

The deputy has proven to be useful in various applications. Depending on use, one distinguishes different kinds of proxy objects:

As a remote proxy is a local representative for an object in another address space is called. It is used for example in network applications or DCOM.

A virtual proxy is used to delay " expensive " operations on the date of actual need. Typical of such expensive operations are the production or the change of a complex object.

To enforce access rights to an object a protection proxy will be used. This is especially useful when accessing different objects should have different access rights to the object to be protected. A concrete example of protection proxies are proxies kernel, which control access to operating system objects.

Alternate, also be used to prevent further operations on the actual access to the object. The object thus remains independent of these operations. For this kind of alternates, the concept of smart References has been established. The reference counting and persistence operations are typical applications.

UML diagram

Client

The client represents the object that accesses by the deputy to the real subject.

Deputy

The deputy has outwardly identical to the real subject interface. He maintains a reference to this and is possibly responsible for its creation and deletion. Other responsibilities arising from the nature of the deputy.

Subject

The subject defines the common interface of proxy and the real subject. Thus, the use of substitutes instead of real subjects is possible.

Real subject

The real subject is the time represented by the proxy object.

Examples

Password protection of some methods within a class, such as class account ( deposit methods and cash-in).

The proxy is a new class ( KontoMitPasswort ) → association to the old account class. The methods in the proxy class to ask the user for a password and then call the methods of the class account ( with correct password).

Java RMI is a way to access remote ( ie running in a different JVM) objects, where the access is no different from the local objects. This is achieved by so-called stubs and skeletons that implement according to the Proxy design pattern, the interface of each corresponding communication partner and forward the method call to this (usually over a network).

Another application example

In situations where multiple copies of a complex object must exist, the proxy design pattern can be combined with the so-called Flyweight design pattern in order to reduce the memory requirements. This typically only one instance of the complex object is created, as well as several smaller proxy objects that refer to this object and act as an interface or deputy. All operations on the proxy objects are forwarded to the original object. If there are no instances of proxies more, as well as the original object can be removed from memory.

Related design patterns

  • Adapter
  • Bridge
  • Decorator
663327
de