Jump to content

Proxy pattern

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by TakuyaMurata (talk | contribs) at 20:41, 3 January 2003. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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

AdapterPattern provides a different interface to its subject. Proxy provides the same interface. DecoratorPattern provides an enhanced interface. [GOF. p216]

As I read the GOF book, the Decorator pattern and the Proxy pattern both provide _the same_ interface to their subject. The difference is that a proxy provides access control to its subject and the decorator adds responsibilities to the subject. -- HansJoachimMatheus

Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests. [GOF, p220]


Simply speaking, a Proxy object is one through which we control access to the actual object on which the functionality lies. Depending on the context in which the Proxy object is used, the Pattern is broadly divided into the following 3 types :

'Virtual Proxy:' Used for Laxy instantiation of objects or for Lazy processing. Suppose you need to support 'resource-hungry' objects (eg. those that involve high amount of I/O or one that involved a database transaction). One need not instantiate these objects until they are really required. The real object would get created only when the client actully requests for some of its functionality.

'Remote Proxy:' Used to hide the communication mechanisms between remote objects. eg. In RMI we have the stubs which act as Remote Proxies for the Skeleton.

'Access Proxy:' Used to provide control over a sensitive master object. This proxy object could check for the client's access permission before allowing methods to be executed on the actual object.

There are also a proxy type called 'Smart Proxy'. This could provide additional functionality over that offered by the exiting object. eg. Suppose an existing class provides a set of non-threadsafe functions. Instead of modifying the existing class to be thread-safe, a smart proxy could be used to synchronize the method calls.

--AbhishekGupta

Your 'Smart Proxy' sounds more like the DecoratorPattern than the ProxyPattern. -- JonathanCarlson



See also: CompositePattern, DecoratorPattern, DesignPatterns


http://home.earthlink.net/~huston2/dp/proxy.html

http://wiki.cs.uiuc.edu/PatternStories/ProxyPattern


CategoryPattern | CategoryStructuralPatterns