Proxy pattern 164856 225730423 2008-07-15T03:17:01Z 168.106.3.8 /* Virtual proxy (in Java) */ [[Image:proxy_pattern_diagram.svg|thumb|400px|Proxy in [[UML]]]] [[Image:Proxy pattern in LePUS3.gif|thumb|400px|Proxy in [[Lepus3|LePUS3]] ([http://lepus.org.uk/ref/legend/legend.xml legend]) ]] 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 another thing. The other thing could be anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. A well-known example of the proxy pattern is a [[reference counting]] [[pointer (computing)|pointer]] object. In situations where multiple copies of a complex object must exist the proxy pattern can be adapted to incorporate the [[Flyweight Pattern]] in order to reduce the application's memory footprint. Typically one instance of the complex object is created, and multiple proxy objects are created, all of which contain a reference to the single original complex object. Any operations performed on the proxies are forwarded to the original object. Once all instances of the proxy are out of scope, the complex object's memory may be deallocated. ==Examples== ===Virtual proxy (in Java)=== The following [[Java (programming language)|Java]] example illustrates the "virtual proxy" pattern. The program's output is: Loading HiRes_10MB_Photo1 Displaying HiRes_10MB_Photo1 Loading HiRes_10MB_Photo2 Displaying HiRes_10MB_Photo2 Displaying HiRes_10MB_Photo2 The <code>ProxyImage</code> class is used to delay the expensive operation of loading a file from disk until the result of that operation is actually needed. If the file is never needed, then the expensive load has been totally eliminated. <source lang="java"> import java.util.*; interface Image { public void displayImage(); } class RealImage implements Image { private String filename; public RealImage(String filename) { this.filename = filename; loadImageFromDisk(); } private void loadImageFromDisk() { // Potentially expensive operation // ... System.out.println("Loading "+filename); } public void displayImage() { System.out.println("Displaying "+filename); } } class ProxyImage implements Image { private String filename; private Image image; public ProxyImage(String filename) { this.filename = filename; } public void displayImage() { if (image == null) { image = new RealImage(filename); // load only on demand } image.displayImage(); } } class ProxyExample { public static void main(String[] args) { Image image1 = new ProxyImage("HiRes_10MB_Photo1"); Image image2 = new ProxyImage("HiRes_10MB_Photo2"); Image image3 = new ProxyImage("HiRes_10MB_Photo2"); image1.displayImage(); // loading necessary image2.displayImage(); // loading necessary image2.displayImage(); // no loading necessary; already done // the third image will never be loaded - time saved! } } </source> ===Protection proxy (in C#)=== In this [[C Sharp (programming language)|C#]] example, the <code>RealClient</code> stores an account number. Only users who know a valid password can access this account number. The <code>RealClient</code> is protected by a <code>ProtectionProxy</code> which knows the password. If a user wants to get an account number, first the proxy asks the user to authenticate; only if the user entered a correct password does the proxy invoke the <code>RealClient</code> to get an account number for the user. In this example, '''thePassword''' is the correct password. <source lang="csharp"> using System; namespace ConsoleApplicationTest.FundamentalPatterns.ProtectionProxyPattern { public interface IClient { string GetAccountNo(); } public class RealClient : IClient { private string accountNo = "12345"; public RealClient() { Console.WriteLine("RealClient: Initialized"); } public string GetAccountNo() { Console.WriteLine("RealClient's AccountNo: " + accountNo); return accountNo; } } public class ProtectionProxy : IClient { private string password; //password to get secret RealClient client; public ProtectionProxy(string pwd) { Console.WriteLine("ProtectionProxy: Initialized"); password = pwd; client = new RealClient(); } // Authenticate the user and return the Account Number public String GetAccountNo() { Console.Write("Password: "); string tmpPwd = Console.ReadLine(); if (tmpPwd == password) { return client.GetAccountNo(); } else { Console.WriteLine("ProtectionProxy: Illegal password!"); return ""; } } } class ProtectionProxyExample { [STAThread] public static void Main(string[] args) { IClient client = new ProtectionProxy("thePassword"); Console.WriteLine(); Console.WriteLine("main received: " + client.GetAccountNo()); Console.WriteLine("\nPress any key to continue . . ."); Console.Read(); } } } </source> ==See also== *[[Composite pattern]] *[[Decorator pattern]] *[[Lazy initialization]] ==External links== *[http://wiki.java.net/bin/view/Javapedia/ProxyPattern Proxy pattern in Java] *[http://www.lepus.org.uk/ref/companion/Proxy.xml Proxy pattern in UML and in LePUS3 (a formal modelling language)] *[http://www.javaworld.com/javaworld/jw-02-2002/jw-0222-designpatterns.html Take control with the Proxy design pattern] by [[David Geary, JavaWorld.com]] *[http://www.fsw.com/Jt/Jt.htm Jt] J2EE Pattern Oriented Framework {{Design Patterns Patterns}} [[Category:Software design patterns]] [[Category:Articles with example Java code]] [[Category:Articles with example C Sharp code]] [[de:Stellvertreter (Entwurfsmuster)]] [[es:Proxy (patrón de diseño)]] [[fr:Proxy (patron de conception)]] [[it:Proxy pattern]] [[ja:Proxy パターン]] [[lt:Proxy (objektas)]] [[ru:Proxy (шаблон проектирования)]] [[uk:Замісник (шаблон проектування)]]