170 likes | 262 Views
Design Patterns: Proxy. Jason Jacob. What is a Proxy?. A Proxy is basically a representative between the Client and the Component. It gives the Client a simple interface to the Component. How can a Proxy be used?. It can be used to control access to restricted Components.
E N D
Design Patterns: Proxy Jason Jacob
What is a Proxy? • A Proxy is basically a representative between the Client and the Component. • It gives the Client a simple interface to the Component.
How can a Proxy be used? • It can be used to control access to restricted Components. • It can be used to provide simpler access to the Component. • It can be used to load data on an “as needed” basis to increase efficiency. • Can be used as a placeholder to enhance efficiency.
Some Code: Simple Math public interface IMath { double Add( double x, double y ); double Sub( double x, double y ); double Mul( double x, double y ); double Div( double x, double y ); } // "RealSubject" class Math implements IMath { public double Add( double x, double y ){ return x + y; } public double Sub( double x, double y ){ return x - y; } public double Mul( double x, double y ){ return x * y; } public double Div( double x, double y ){ return x / y; } }
Some More Code: Math Proxy class MathProxy implements IMath { Math math; private double oldAddx = 0.0; private double oldAddy = 0.0; private double oldSum = 0.0; // more fields here for other operations... public MathProxy() { } public double Add( double x, double y ) { if (x == oldAddx && y == oldAddy) { return oldSum; } else { oldAddx = x; oldAddy = y; oldSum = math.Add(x,y); return oldSum; } } // more methods here for other operations... }
Even More Code: The Client public class ProxyApp { public static void main(String[] args) { MathProxy p = new MathProxy(); System.out.println( "4 + 2 = ", p.Add( 4, 2 ) ); System.out.println( "4 + 2 = ", p.Add( 4, 2 ) ); System.out.println( "5 + 2 = ", p.Add( 5, 2 ) ); System.out.println( "4 - 2 = ", p.Sub( 4, 2 ) ); System.out.println( "4 * 2 = ", p.Mul( 4, 2 ) ); System.out.println( "4 / 2 = ", p.Div( 4, 2 ) ); } }
In short… • The Client needed to get something done. • The client calls the method(s) of the Proxy. • The Proxy may do some internal pre-processing and sends the real call to the Component. • The Component sends back results. • The Proxy may do some internal post-processing and sends the results back to the Client.
Some Proxy Implementations • Protection Proxy: Controls access to Component based on the access rights of Clients. • Cache Proxy: Saves results temporarily so when Client requests same expensive operation again, the saved results are returned without making call to Component. • Virtual Proxy: Expensive objects are created on demand.
Advantages • Efficiency is generally enhanced and cost is lowered. • Prevents Clients from accessing server Components directly. • Keeps the housekeeping and functionality separate.
Disadvantages • Efficiency can suffer due to indirection. • Implementations can be complex.
Sources • http://www.openloop.com/softwareEngineering/patterns/designPattern/dPattern_Proxy.htm • http://www.inf.bme.hu/ooret/1999osz/DesignPatterns/Proxy4/