Proxy

720 views
674 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
720
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Proxy

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

×