<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Proxy_pattern</id>
	<title>Proxy pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Proxy_pattern"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Proxy_pattern&amp;action=history"/>
	<updated>2026-04-21T02:26:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Proxy_pattern&amp;diff=118960&amp;oldid=prev</id>
		<title>~2025-31256-79: Constructor must be marked explicit to prevent implicit conversions. Undid revision 1320462120 by ClueBot NG (talk)</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Proxy_pattern&amp;diff=118960&amp;oldid=prev"/>
		<updated>2025-11-04T20:57:02Z</updated>

		<summary type="html">&lt;p&gt;Constructor must be marked explicit to prevent implicit conversions. Undid revision &lt;a href=&quot;/index.php/Special:Diff/1320462120&quot; title=&quot;Special:Diff/1320462120&quot;&gt;1320462120&lt;/a&gt; by &lt;a href=&quot;/index.php/Special:Contributions/ClueBot_NG&quot; title=&quot;Special:Contributions/ClueBot NG&quot;&gt;ClueBot NG&lt;/a&gt; (&lt;a href=&quot;/index.php?title=User_talk:ClueBot_NG&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User talk:ClueBot NG (page does not exist)&quot;&gt;talk&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{short description|Software design pattern}}&lt;br /&gt;
In [[computer programming]], the &amp;#039;&amp;#039;&amp;#039;proxy pattern&amp;#039;&amp;#039;&amp;#039; is a [[software design pattern]]. A &amp;#039;&amp;#039;proxy&amp;#039;&amp;#039;, 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.&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
The Proxy &lt;br /&gt;
&amp;lt;ref name=&amp;quot;GoF&amp;quot;&amp;gt;{{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}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
design pattern is one of the twenty-three well-known &lt;br /&gt;
&amp;#039;&amp;#039;[[Design Patterns|GoF design patterns]]&amp;#039;&amp;#039; &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Problem===&lt;br /&gt;
The proxy design pattern is used to solve such problems:&amp;lt;ref&amp;gt;{{cite web|title=The Proxy design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=s07&amp;amp;ugr=proble|website=w3sDesign.com|access-date=2017-08-12}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* The access to an object should be controlled.&lt;br /&gt;
* Additional functionality should be provided when accessing an object.&lt;br /&gt;
&lt;br /&gt;
When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights.&lt;br /&gt;
&lt;br /&gt;
===Solution===&lt;br /&gt;
&lt;br /&gt;
Define a separate &amp;lt;code&amp;gt;Proxy&amp;lt;/code&amp;gt; object that &lt;br /&gt;
* can be used as a substitute for another object (&amp;lt;code&amp;gt;Subject&amp;lt;/code&amp;gt;), and &lt;br /&gt;
* implements additional functionality to control the access to this subject.&lt;br /&gt;
&lt;br /&gt;
This makes it possible to work through a &amp;lt;code&amp;gt;Proxy&amp;lt;/code&amp;gt; object to perform additional functionality when accessing a subject, like checking the access rights of clients accessing a sensitive object.&lt;br /&gt;
&lt;br /&gt;
To act as a substitute for a subject, a proxy must implement the &amp;lt;code&amp;gt;Subject&amp;lt;/code&amp;gt; interface. Clients can&amp;#039;t tell whether they work with a subject or its proxy.&lt;br /&gt;
 &lt;br /&gt;
See also the UML class and sequence diagram below.&lt;br /&gt;
&lt;br /&gt;
==Structure==&lt;br /&gt;
=== UML class and sequence diagram ===&lt;br /&gt;
[[File:w3sDesign Proxy Design Pattern UML.jpg|frame|none|A sample UML class and sequence diagram for the Proxy design pattern. &lt;br /&gt;
&amp;lt;ref&amp;gt;{{cite web|title=The Proxy design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=s07&amp;amp;ugr=struct|website=w3sDesign.com|access-date=2017-08-12}}&amp;lt;/ref&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
In the above [[Unified Modeling Language|UML]] [[class diagram]], &lt;br /&gt;
the &amp;lt;code&amp;gt;Proxy&amp;lt;/code&amp;gt; class implements the &amp;lt;code&amp;gt;Subject&amp;lt;/code&amp;gt; interface so that it can act as substitute for &amp;lt;code&amp;gt;Subject&amp;lt;/code&amp;gt; objects. It maintains a reference (&amp;lt;code&amp;gt;realSubject&amp;lt;/code&amp;gt;) &lt;br /&gt;
to the substituted object (&amp;lt;code&amp;gt;RealSubject&amp;lt;/code&amp;gt;) so that it can forward requests to it&lt;br /&gt;
(&amp;lt;code&amp;gt;realSubject.operation()&amp;lt;/code&amp;gt;). &lt;br /&gt;
&lt;br /&gt;
The sequence diagram &lt;br /&gt;
shows the run-time interactions: The &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; object &lt;br /&gt;
works through a &amp;lt;code&amp;gt;Proxy&amp;lt;/code&amp;gt; object that&lt;br /&gt;
controls the access to a &amp;lt;code&amp;gt;RealSubject&amp;lt;/code&amp;gt; object.&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Proxy&amp;lt;/code&amp;gt; forwards the request to the &amp;lt;code&amp;gt;RealSubject&amp;lt;/code&amp;gt;, which performs the request.&lt;br /&gt;
&lt;br /&gt;
=== Class diagram ===&lt;br /&gt;
[[File:proxy_pattern_diagram.svg|frame|none|Proxy in [[Unified Modeling Language|UML]]]]&lt;br /&gt;
&lt;br /&gt;
[[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])]]&lt;br /&gt;
&lt;br /&gt;
== Possible usage scenarios ==&lt;br /&gt;
===Remote proxy===&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Virtual proxy===&lt;br /&gt;
{{See|Lazy loading}}&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Protection proxy===&lt;br /&gt;
A protection proxy might be used to control access to a resource based on access rights.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
The following is an example of the proxy design pattern in [[C++]]:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
using std::string;&lt;br /&gt;
using std::unique_ptr;&lt;br /&gt;
&lt;br /&gt;
// Subject Interface&lt;br /&gt;
class Image {&lt;br /&gt;
public:&lt;br /&gt;
    virtual void display() const = 0;&lt;br /&gt;
    virtual ~Image() = default;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Real Subject (expensive to load)&lt;br /&gt;
class RealImage : public Image {&lt;br /&gt;
private:&lt;br /&gt;
    string filename;&lt;br /&gt;
public:&lt;br /&gt;
    explicit RealImage(const string&amp;amp; file):&lt;br /&gt;
        filename{file} {&lt;br /&gt;
        loadFromDisk();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    void display() const override {&lt;br /&gt;
        std::println(&amp;quot;Displaying image: {}&amp;quot;, filename);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
private:&lt;br /&gt;
    void loadFromDisk() const {&lt;br /&gt;
        std::println(&amp;quot;Loading image from disk: {}&amp;quot;, filename);&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Proxy (controls access to RealImage)&lt;br /&gt;
class ProxyImage : public Image {&lt;br /&gt;
private:&lt;br /&gt;
    string filename;&lt;br /&gt;
    mutable unique_ptr&amp;lt;RealImage&amp;gt; realImage; // lazily created&lt;br /&gt;
public:&lt;br /&gt;
    explicit ProxyImage(const string&amp;amp; file):&lt;br /&gt;
        filename{file} {}&lt;br /&gt;
&lt;br /&gt;
    void display() const override {&lt;br /&gt;
        if (!realImage) {&lt;br /&gt;
            std::println(&amp;quot;(Proxy) Loading image on demand...&amp;quot;);&lt;br /&gt;
            realImage = std::make_unique&amp;lt;RealImage&amp;gt;(filename);&lt;br /&gt;
        }&lt;br /&gt;
        realImage-&amp;gt;display();&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Client code&lt;br /&gt;
int main() {&lt;br /&gt;
    Image* img = new ProxyImage(&amp;quot;photo.png&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    // First display (should load)&lt;br /&gt;
    img-&amp;gt;display();&lt;br /&gt;
&lt;br /&gt;
    // Second display (should use cached image)&lt;br /&gt;
    img-&amp;gt;display();&lt;br /&gt;
&lt;br /&gt;
    delete img;&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Composite pattern]]&lt;br /&gt;
* [[Decorator pattern]]&lt;br /&gt;
* [[Lazy initialization]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
{{Wikibooks|Computer Science Design Patterns|Proxy|Proxy implementations in various languages}}&lt;br /&gt;
{{Commons category}}&lt;br /&gt;
* {{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}}&lt;br /&gt;
* [https://perfectjpattern.sourceforge.net/dp-proxy.html PerfectJPattern Open Source Project], Provides componentized implementation of the Proxy Pattern in Java&lt;br /&gt;
* {{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}}&lt;br /&gt;
* [https://sourcemaking.com/design_patterns/proxy Proxy Design Pattern]&lt;br /&gt;
* {{webarchive |url=https://web.archive.org/web/20141019100543/http://patterns.org.pl/proxy.html |title=Proxy pattern C++ implementation example}}&lt;br /&gt;
* [https://wiki.c2.com/?ProxyPattern Proxy pattern description from the Portland Pattern Repository]&lt;br /&gt;
&lt;br /&gt;
{{Design Patterns Patterns}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Software design patterns]]&lt;br /&gt;
[[Category:Articles with example C Sharp code]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;/div&gt;</summary>
		<author><name>~2025-31256-79</name></author>
	</entry>
</feed>