Thursday, November 18, 2010

Proxy Design Pattern - Structural

1.1     Definition

- Provides a representative object (proxy) that controls access to another similar object.
- Provide a surrogate or placeholder for another object to control access to it.

1.2     Intent

  • Proxy a surrogate or placeholder for another object to control access to it.
  • Proxy pattern describe how to provide a level of indirection to an object, and the implementations of the proxy object keep a reference to another object to which they forwards requests.
  • Proxy pattern composes an object and provides an identical interface to clients.
  • Its intent is to provide a stand-in for a subject when it's inconvenient or undesirable to access the subject directly because, for example, it lives on a remote machine, has restricted access, or is persistent.
  • In the Proxy pattern , the subject defines the key functionality, and the proxy provides (or refuses) access to it.
  • Proxy focuses on one relationship - between the proxy and its subject - and that relationship can be expressed statically.

1.3     Motivation

One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it.

1.4     Applicability

Proxy is applicable whenever there is a need for a versatile or sophisticated reference to an object than a simple pointer. There are several cases where a Proxy can be useful.

  1. An object, such as a large image, takes a long time to load.
  2. The results of a computation take a long time to complete, and you need to display intermediate results while the computation continues.
  3. The object is on a remote machine, and loading it over the network may be slow, especially during peak network load periods.
  4. The object has limited access rights, and the proxy can validate the access permissions for that user.

1.5     Structure

1.6     Participants

The classes and/or objects participating in this pattern are:

  • Proxy
    • Maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
    • Provides an interface identical to Subject's so that a proxy can be substituted for for the real subject.
    • Controls access to the real subject and may be responsible for creating and deleting it.
  • Subject
    • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • RealSubject
    • defines the real object that the proxy represents.

1.7     Other Proxy Patterns

Remote Proxies
Provides a reference to an object located in a different address space on the same or different machine.

Responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.

Virtual Proxies
Allows the creation of a memory intensive object on demand. The object will not be created until it is really needed

May cache additional information about the real subject so that they can postpone accessing it.

Protection Proxies
Check that the caller has the access permissions required to perform a request.


Is an optimization pattern.

The fundamental idea is that if multiple callers ask for objects which are initially indistinguishable, you can give them pointers to the same object. This function can be maintained until a caller tries to modify its "copy" of the object, at which point a true private copy is created to prevent the changes becoming visible to everyone else.

All of this happens transparently to the callers. The primary advantage is that if a caller never makes any modifications, no private copy need ever be created.

The COW concept is also used in virtualization/emulation software such as Bochs, QEMU, and UML for virtual disk storage. This allows a great reduction in required disk space when multiple VMs can be based on the same hard disk image, as well as increased performance as disk reads can be cached in RAM and subsequent reads served to other VMs out of the cache.

Cache proxy
Provides temporary storage of the results of expensive target operations so that multiple clients can share the results

Firewall proxy
Protects targets from bad clients

1.8     Sample Code

This structural code demonstrates the Proxy pattern which provides a representative object (proxy) that controls access to another similar object.

using System;

namespace Xpanxion.Structural.Proxy

    class MainApp
        static void Main()
            Proxy proxy = new Proxy();

    abstract class Subject
        public abstract void Request();

    class RealSubject : Subject
        public override void Request()
            Console.WriteLine("Called RealSubject.Request()");

    class Proxy : Subject
        RealSubject realSubject;

        public override void Request()
            // Use 'lazy initialization'
            if (realSubject == null)
                realSubject = new RealSubject();


1.9     Proxies in C#

You see more proxy- like behavior in C# than in other languages, because it is crafted for network and Internet use. For example, the ADO.Net database connection classes are all effectively proxies.

Hope this helps.

Thanks & Regards,
Arun Manglick

No comments:

Post a Comment