Proxy
Upcoming SlideShare
Loading in...5
×
 

Proxy

on

  • 874 views

 

Statistics

Views

Total Views
874
Views on SlideShare
872
Embed Views
2

Actions

Likes
0
Downloads
30
Comments
0

2 Embeds 2

http://www.slideshare.net 1
http://freesslcertificate.me 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Proxy Proxy Presentation Transcript

  • 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.
  • Simple Example on Efficiency
  • The Class Diagram
  • 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 ) );
    • }
    • }
  • Sequence Diagram
  • 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.
  • Firewall Proxy
  • Firewall Proxy
  • 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/