Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Proxy pattern
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Software design pattern}} In [[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 (object-oriented programming)|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. ==Overview== The Proxy <ref name="GoF">{{cite book|author=Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides|title=Design Patterns: Elements of Reusable Object-Oriented Software|year=1994|publisher=Addison Wesley|isbn=0-201-63361-2|pages=[https://archive.org/details/designpatternsel00gamm/page/207 207ff]|url-access=registration|url=https://archive.org/details/designpatternsel00gamm/page/207}}</ref> design pattern is one of the twenty-three well-known ''[[Design Patterns|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?=== Source:<ref>{{cite web|title=The Proxy design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=s07&ugr=proble|website=w3sDesign.com|access-date=2017-08-12}}</ref> * The access to an object should be controlled. * Additional functionality should be provided when accessing an object. 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 <code>Proxy</code> object that * can be used as a substitute for another object (<code>Subject</code>), and * implements additional functionality to control the access to this subject. This makes it possible to work through a <code>Proxy</code> 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 <code>Subject</code> interface. Clients can't tell whether they work with a subject or its proxy. See also the UML class and sequence diagram below. ==Structure== === UML class and sequence diagram === [[File:w3sDesign Proxy Design Pattern UML.jpg|frame|none|A sample UML class and sequence diagram for the Proxy design pattern. <ref>{{cite web|title=The Proxy design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=s07&ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}</ref>]] In the above [[Unified Modeling Language|UML]] [[class diagram]], the <code>Proxy</code> class implements the <code>Subject</code> interface so that it can act as substitute for <code>Subject</code> objects. It maintains a reference (<code>realSubject</code>) to the substituted object (<code>RealSubject</code>) so that it can forward requests to it (<code>realSubject.operation()</code>). The sequence diagram shows the run-time interactions: The <code>Client</code> object works through a <code>Proxy</code> object that controls the access to a <code>RealSubject</code> object. In this example, the <code>Proxy</code> forwards the request to the <code>RealSubject</code>, which performs the request. === Class diagram === [[File:proxy_pattern_diagram.svg|frame|none|Proxy in [[Unified Modeling Language|UML]]]] [[File:Proxy pattern in LePUS3.gif|frame|none|Proxy in [[Lepus3|LePUS3]] ([https://web.archive.org/web/20180314162121/http://www.lepus.org.uk/ref/legend/legend.xml legend])]] == Possible usage scenarios == ===Remote proxy=== 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 [[Automated teller machine|ATM]] implementation, where the ATM might hold proxy objects for bank information that exists in the remote server. ===Virtual proxy=== {{See|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. ===Protection proxy=== A protection proxy might be used to control access to a resource based on access rights. ==See also== * [[Composite pattern]] * [[Decorator pattern]] * [[Lazy initialization]] ==References== {{Reflist}} ==External links== {{Wikibooks|Computer Science Design Patterns|Proxy|Proxy implementations in various languages}} {{Commons category}} * {{cite web |last1=Geary |first1=David |date=2002-02-22 |df=mdy |url=https://www.infoworld.com/article/2074068/take-control-with-the-proxy-design-pattern.html |title=Take control with the Proxy design pattern |work=[[JavaWorld]] |access-date=2020-07-20}} * [http://perfectjpattern.sourceforge.net/dp-proxy.html PerfectJPattern Open Source Project], Provides componentized implementation of the Proxy Pattern in Java * {{webarchive |url=https://web.archive.org/web/20120311202925/http://www.netobjectives.com/PatternRepository/index.php?title=AdapterVersusProxyVersusFacadePatternComparison |title=Adapter vs. Proxy vs. Facade Pattern Comparison}} * [https://sourcemaking.com/design_patterns/proxy Proxy Design Pattern] * {{webarchive |url=https://web.archive.org/web/20141019100543/http://patterns.org.pl/proxy.html |title=Proxy pattern C++ implementation example}} * [https://wiki.c2.com/?ProxyPattern Proxy pattern description from the Portland Pattern Repository] {{Design Patterns Patterns}} [[Category:Software design patterns]] [[Category:Articles with example C Sharp code]] [[Category:Articles with example Java code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Commons category
(
edit
)
Template:Design Patterns Patterns
(
edit
)
Template:Reflist
(
edit
)
Template:See
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Webarchive
(
edit
)
Template:Wikibooks
(
edit
)