Proxy patternIn computer programming, the proxy pattern is a software design pattern. 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. OverviewThe Proxy [1] design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. What problems can the Proxy design pattern solve? [2]
When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights. What solution does the Proxy design pattern describe?Define a separate
This makes it possible to work through a To act as substitute for a subject, a proxy must implement the See also the UML class and sequence diagram below. StructureUML class and sequence diagramIn the above UML class diagram,
the The sequence diagram
shows the run-time interactions: The Class diagramPossible usage scenariosRemote proxyIn 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 proxyIn 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 proxyA protection proxy might be used to control access to a resource based on access rights. See alsoReferences
External linksThe Wikibook Computer Science Design Patterns has a page on the topic of: Proxy implementations in various languages Wikimedia Commons has media related to Proxy pattern.
|