1 / 16

Design Patterns: Proxy

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.

Télécharger la présentation

Design Patterns: Proxy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns: Proxy Jason Jacob

  2. 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.

  3. 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.

  4. Simple Example on Efficiency

  5. The Class Diagram

  6. 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; } }

  7. 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... }

  8. 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 ) ); } }

  9. Sequence Diagram

  10. 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.

  11. 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.

  12. Firewall Proxy

  13. Firewall Proxy

  14. Advantages • Efficiency is generally enhanced and cost is lowered. • Prevents Clients from accessing server Components directly. • Keeps the housekeeping and functionality separate.

  15. Disadvantages • Efficiency can suffer due to indirection. • Implementations can be complex.

  16. Sources • http://www.openloop.com/softwareEngineering/patterns/designPattern/dPattern_Proxy.htm • http://www.inf.bme.hu/ooret/1999osz/DesignPatterns/Proxy4/

More Related