Suvarna Garge (Editor)

Proxy pattern

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Proxy pattern

In computer programming, the proxy pattern is a software design pattern.

Contents

A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.

Remote Proxy

In distributed object communication, a local object represents a remote object (one that belongs to a different address space). The local object is a proxy for the remote object, and method invocation on the local object results in remote method invocation on the remote object. An example would be an ATM implementation, where the ATM might hold proxy objects for bank information that exists in the remote server.

Virtual Proxy

In place of a complex or heavy object, a skeleton representation may be advantageous in some cases. When an underlying image is huge in size, it may be represented using a virtual proxy object, loading the real object on demand.

Protection Proxy

A protection proxy might be used to control access to a resource based on access rights.

C#

Output

Sorry, the driver is too young to drive. Car has been driven!

Notes:

  • A proxy may hide information about the real object to the client.
  • A proxy may perform optimization like on demand loading.
  • A proxy may do additional house-keeping job like audit tasks.
  • Proxy design pattern is also known as surrogate design pattern.
  • Crystal

    Output

    Sorry, the driver is too young to drive. Car has been driven!

    Delphi / Object Pascal

    Usage

    Output

    Sorry, the driver is too young to drive. Car has been driven!

    Java

    The following Java example illustrates the "virtual proxy" pattern. The ProxyImage class is used to access a remote method.

    The example creates first an interface against which the pattern creates the classes. This interface contains only one method to display the image, called displayImage(), that has to be coded by all classes implementing it.

    The proxy class ProxyImage is running on another system than the real image class itself and can represent the real image RealImage over there. The image information is accessed from the disk. Using the proxy pattern, the code of the ProxyImage avoids multiple loading of the image, accessing it from the other system in a memory-saving manner. It should be noted, however, that the lazy loading demonstrated in this example is not part of the proxy pattern, but is merely an advantage made possible by the use of the proxy.

    The program's output is:

    Loading HiRes_10MB_Photo1 Displaying HiRes_10MB_Photo1 Displaying HiRes_10MB_Photo1 Loading HiRes_10MB_Photo2 Displaying HiRes_10MB_Photo2 Displaying HiRes_10MB_Photo2 Displaying HiRes_10MB_Photo1

    References

    Proxy pattern Wikipedia