Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROXY Matt Burden T-Kiang Tan Proxy (prok-se): An agent or substitute authorized to act for another SourceSource: Webster's Revised Unabridged Dictionary,

Similar presentations


Presentation on theme: "PROXY Matt Burden T-Kiang Tan Proxy (prok-se): An agent or substitute authorized to act for another SourceSource: Webster's Revised Unabridged Dictionary,"— Presentation transcript:

1 PROXY Matt Burden T-Kiang Tan Proxy (prok-se): An agent or substitute authorized to act for another SourceSource: Webster's Revised Unabridged Dictionary, © 1996 MICRA, Inc.

2 Pattern Type: Object Structural Intent: Provide a surrogate or placeholder for another object to control access to it.

3 Examples Debit Card (represents the cash in your account when making a purchase). Proxy voting (like an absentee ballot or stock owner proxy).

4 Design Issues In terms of time and resources (memory), it is expensive to instantiate objects before we use them. May need to protect access to an object. Contexts:

5 Proxy allows for objects to be created on demand. Proxy uses another object in place or a stand-in for the actual object. Proxy can act just like the object and takes care of instantiating it when required. Proxy Design Solutions

6 Absentee Ballot Proxy

7 Debit Card Example

8 Debit Card UML

9 Card Swiper UML

10

11

12 Proxy Consequences Can hide the fact that an object resides in a different address space (remote). Can enhance optimization by creating objects on demand (virtual). Can act as a pointer (smart-reference). Can control access to the original object (protection).

13 Proxy Benefits Smaller memory footprint—loads as needed Changes can be made to the actual object with making any changes to the application (i.e. swapping pictures).

14 Proxy Liabilities When proxy is first used, user may have to wait for file to load. Work Around: Have a process load it when application is started. Code management becomes more complicated (more files). Work Around: Document source code and processes.

15 Questions?

16 //Title: UsingPlastic //Version: //Copyright: Copyright (c) 1999 //Author: T-Kiang Tan //Company: University of Chicago: CSPP 523 //Description: Demo. package UsingPlastic; import java.util.*; //Using ArrayList and HashTable public class PlasticProxy implements PlasticProxyInterface { ArrayList list; String[] card_types; public PlasticProxy(String[] cardTypes) { card_types = cardTypes; list = new ArrayList(card_types.length); } public String getApproval(String card_type)

17 { System.out.println("****PlasticProxy: Connecting to " + card_type); Plastic plastic= new Plastic(card_type); System.out.println("****PlasticProxy: " + plastic.initialize()); System.out.println("****PlasticProxy: Requesting for approval from " + card_type); System.out.println("****PlasticProxy: " + plastic.getApproval(card_type)); return("Transaction has been approved"); } public String initialize() { System.out.println("****PlasticProxy: Initializing proxy. done only once"); return(new String("PlasticProxy is initialized")); }

18 //Title: UsingPlastic //Version: //Copyright: Copyright (c) 1999 //Author: T-Kiang Tan //Company: University of Chicago: CSPP 523 //Description: Demo. package UsingPlastic; public class Plastic implements PlasticProxyInterface { String card; public Plastic(String card_type){ card = card_type;} public String getApproval(String card_type) { System.out.println("******" + card + ": Request is in progress"); return(card + " - Transaction Approved"); }

19 public String initialize() { System.out.println("******" +card + ": Initializing is in progress"); return("Connection Established with " + card); } //Title: UsingPlastic //Version: //Copyright: Copyright (c) 1999 //Author: T-Kiang Tan //Company: University of Chicago: CSPP 523 //Description: Demo. package UsingPlastic; public interface PlasticProxyInterface { public String getApproval(String card_type); public String initialize(); }

20 //Title: UsingPlastic //Version: //Copyright: Copyright (c) 1999 //Author: T-Kiang Tan //Company: University of Chicago: CSPP 523 //Description:Demo. package UsingPlastic; import java.awt.*; import java.awt.event.*; import javax.swing.*;

21 public class UsingPlastic { public static void main(String[] card_types) { String input = " "; String[] tokens; int i = 0; PlasticProxy plasticproxy = new PlasticProxy(card_types); System.out.println("**UsingPlastic: " + plasticproxy.initialize()); while (true) { System.out.println("Main: Please slide the card when ready:"); for(i = 0; i < card_types.length; i++) System.out.println("Main: " + (i+1) + ". " + card_types[i]); System.out.print("Main: Which card did you slide again? >> "); input = ParserUtils.getKeyInput();

22 if(input.equalsIgnoreCase("exit")) break; Integer integer = new Integer(input); i = integer.intValue() - 1; System.out.println("#########################"); System.out.println("**UsingPlastic: Requesting for approval from " + card_types[i]); System.out.println("**UsingPlastic: " + plasticproxy.getApproval(card_types[i]) + " by " + card_types[i]); System.out.println("#########################"); } System.out.println("Main: Thanks you for using the Proxy Presentation Demo"); System.out.println("Main: Matt Burden and T-Kiang Tan"); System.out.println("Main: Winter 2002"); }

23 //Parser utils--this is our first class which does not have a main method. Its purpose it to serve as a warehouse of related methods that can be accessed from any main//method. This class contains methods useful for writing a parser. // 1. public static String getKeyInput(){ //blocks program until user enters zero or more characters followed by a "enter“ and returns input to calling program // 2. public static String[] getTokens(String input){ //takes String and breaks into tokens defined by one or more white spaces and return array of String each element of which is an individual tokenpackage UsingPlastic; import java.io.*; import java.util.*; class ParserUtils{public static String getKeyInput(){ String input = null; try{BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); input = in.readLine();} catch (IOException ioe){System.out.println(ioe);} return input; } public static String[] getTokens(String input){ int i = 0;

24 StringTokenizer st = new StringTokenizer(input); int numTokens = st.countTokens(); String[] tokenList = new String[numTokens]; while (st.hasMoreTokens()){ tokenList[i] = st.nextToken(); i++;} return(tokenList); }}


Download ppt "PROXY Matt Burden T-Kiang Tan Proxy (prok-se): An agent or substitute authorized to act for another SourceSource: Webster's Revised Unabridged Dictionary,"

Similar presentations


Ads by Google