The 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.
When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights.
Define a separate Proxy object that
This makes it possible to work through a Proxy object to perform additional functionality when accessing a subject, like checking the access rights of clients accessing a sensitive object.
To act as a substitute for a subject, a proxy must implement the Subject interface. Clients can't tell whether they work with a subject or its proxy.
See also the UML class and sequence diagram below.
In the above UML class diagram, the Proxy class implements the Subject interface so that it can act as substitute for Subject objects. It maintains a reference (realSubject) to the substituted object (RealSubject) so that it can forward requests to it (realSubject.operation()).
The sequence diagram shows the run-time interactions: The Client object works through a Proxy object that controls the access to a RealSubject object. In this example, the Proxy forwards the request to the RealSubject, which performs the request.
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.
Further information: Lazy loading
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.
A protection proxy might be used to control access to a resource based on access rights.
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 207ff. ISBN 0-201-63361-2.{{cite book}}: CS1 maint: multiple names: authors list (link) 0-201-63361-2 ↩
"The Proxy design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-12. http://w3sdesign.com/?gr=s07&ugr=proble ↩