Pattern:
Proxy


Author

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

Intent

"Provide a surrogate or placeholder for another object to control access to it." (Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Reading, MA: Addison-Wesley, 1995)

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. Consider a document editor that can embed graphical objects in a document. Some graphical objects, like large raster images, can be expensive to create. But opening a document should be fast, so we should avoid creating all the expensive objects at once when the document is opened. This isnt necessary anyway, because not all of these objects will be visible in the document at the same time. The proxy object acts as a stand-in for the real image. The proxy acts just like the image and takes care of instantiating it when its required. The image proxy creates the real image only when the document editor asks it to display itself by invoking its Draw operation. The proxy forwards subsequent requests directly to the image. It must keep a reference to the image after creating it. For example, the document editor accesses embedded images through the interface defined by the abstract Graphic class (Subject in the thumbnail). ImageProxy (Proxy in the thumbnail) is a class for images that are created on demand. ImageProxy maintains the file name as a reference to the image on disk. The file name is passed as an argument to the ImageProxy constructor. ImageProxy also stores the bounding box of the image and a reference to the real Image (RealSubject in the thumbnail) instance. This reference wont be valid until the proxy instantiates the real image. The Draw operation (a method in ImageProxy) makes sure the image is instantiated before forwarding it the request. GetExtent (a method in ImageProxy) forwards the request to the image only if its instantiated; otherwise, ImageProxy returns the extent (i.e. width and height) it stores.

Known Uses

This pattern has been used on the following systems: Used with the Forwarder-Receiver pattern to implement the stub concept. (Lohr, K.-P., I. Piens, and T. Wolff, Verteilungstransparenz bei der objektorientierten Entwicklung verteilter Applikationen, OBJEKTspektrum 5/1994, pp. 8-14, SIGS Publications, Munchen, Germany, 1994.) The NeXTSTEP operating system uses the Proxy pattern to provide local stubs for remote objects. (Addison-Wesley, Reading, MA. NEXTSTEP General Reference: Release 3, Volumes 1 and 2, 1994.) OMG-CORBA uses the Proxy pattern in two ways. First, the IDL-stubs (client-stubs) guard clients against the concrete implementation of their servers and the Object Request Broker. Second, the Object Request Broker uses IDL-skeletons to forward requests to concrete remote server components. (Object Management Group, The Common Object Request Broker: Architecture and Specification, OMG Document Number 91.12.1, Revision 1.1, 1992.) The concrete OMG-CORBA implementation, Orbix, uses remote proxies. By specifying its unique identifier, a client can bind to an original. (IONA Technologies Ltd., Orbix Programmers Guide, compare also http://www.iona.ie/, Dublin, Ireland, 1995.) World Wide Web Proxy generally runs on a firewall machine and allows people inside the firewall concurrent access to the outside world. (Luotonen, A. and K. Altis, World Wide Web Proxies, WWW94 Conference, 1994, see also http://www.w3.org/pub/WWW/Daemon/) In Microsofts OLE, proxies hide whether a specific server is local or remote from a client. The servers may be libraries dynamically linked to the clients address space, or as separate processes. (Booch, G., Object-Oriented Analysis and Design With Applications, Second Edition, Benjamin/Cummings, Redwood City, California, 1994.)

See Also

Adapter and Decorator patterns

Thumbnail

Keywords

caching, Decorator pattern, Proxy pattern, structural patterns, Gamma patterns, Adapter Class pattern, Adapter Object pattern, Surrogate pattern, forward requests, object (defer creation), proxy, surrogate, object (access control)

Business Domains

any system requiring abstraction for object creationany system requiring control over object instancingany system requiring local or distributed access to objects

Problem Forces

need to control access to another objectneed to provide abstraction between subsystem and client

Benefits

enhanced maintainability and extensibility of the systemmay reduce compilation dependenciespromotes layering of systemsprovides weak coupling between clients and subsystems

Implementation Files

Test driver Test.javaSimple implementation of the pattern - Subject class. Subject.javaSimple implementation of the pattern - RealSubject class. RealSubject.javaSimple implementation of the pattern - Proxy class. Proxy.javaTest driver testproxy.cppSimple implementation of the pattern. proxy.h



Generated on Fri Oct 20 10:17:57 GMT+02:00 2000 by Framework Studio