Unformatted text preview:

Proxy PatternKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/6448 — Lecture 29 — 12/04/2007© University of Colorado, 20071Wednesday, December 5, 2007Lecture Goals• Cover Material from Chapter 11 of the Design Patterns Textbook• Proxy Pattern2Wednesday, December 5, 2007Proxy Pattern: Definition• The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.• Use the Proxy Pattern to create a representative object that controls access to another object, which may be remote, expensive to create, or in need of securing• We’ll see several examples of the Proxy pattern in use in this lecture, including the Remote Proxy, Virtual Proxy, and Protection Proxy3Wednesday, December 5, 2007Proxy Pattern: Structurerequest()Subject«interface»request()RealSubjectrequest()ProxysubjectA Proxy and “Real” Subject class both implement a Subject interface. Clients interact with the Proxy, which controls access to the methods of the RealSubject class. When appropriate, the Proxy will forward requests to the RealSubject via delegation. The Proxy may also respond to client requests itself, or it may reject the request (perhaps by throwing an exception).4Wednesday, December 5, 2007Gumball Revisited• To illustrate the Remote Proxy variation of the Proxy Pattern (in which the Proxy and RealSubject objects are on different machines), we return to the Gumball Machine example of the previous lecture• Our client would like a way to monitor gumball machines remotely• to enable regular status reports and to allow them to do a better job of keeping the gumball machines full of gumballs5Wednesday, December 5, 2007Step 1: Update Gumball Machinepublic class GumballMachine {12 State soldOutState;3 State noQuarterState;4 State hasQuarterState;5 State soldState;6 State winnerState;7 8 State state = soldOutState;9 int count = 0;10 String location;11 12 public GumballMachine(String location, int count) {13 soldOutState = new SoldOutState(this);14 noQuarterState = new NoQuarterState(this);15 hasQuarterState = new HasQuarterState(this);16 soldState = new SoldState(this);17 winnerState = new WinnerState(this);1819 this.count = count;20 if (count > 0) {21 state = noQuarterState;22 } 23 this.location = location;24 }25 26 public void insertQuarter() {27 state.insertQuarter();28 }29 30 public void ejectQuarter() {31 state.ejectQuarter();32 }33 34 public void turnCrank() {35 state.turnCrank();36 state.dispense();37 }3839 void setState(State state) {40 this.state = state;41 }42 43 void releaseBall() {44 System.out.println("A gumball comes rolling out the slot...");45 if (count != 0) {46 count = count - 1;47 }48 }49 50 public int getCount() {51 return count;52 }5354 public void refill(int count) {55 this.count = count;56 state = noQuarterState;57 }5859 public State getState() {60First, we update the Gumball Machine class to store its locationWe also add a getter method for this attribute (not shown)6Wednesday, December 5, 2007Step 2: Create a Gumball Monitor Classpublic class GumballMonitor {1 GumballMachine machine;2 3 public GumballMonitor(GumballMachine machine) {4 this.machine = machine;5 }6 7 public void report() {8 System.out.println("Gumball Machine: " + machine.getLocation());9 System.out.println("Current inventory: " + machine.getCount() + " gumballs");10 System.out.println("Current state: " + machine.getState());11 }12}1314Simple! The monitor takes an instance of the Gumball machine class and can generate a status report: location, number of gumballs, and the machine’s current state.But something is wrong with this design… what?7Wednesday, December 5, 2007Going Remote• The Gumball Monitor is coded to accept a pointer to a local Gumball Machine object• But we need to monitor Gumball Machines that are not physically present• Or in computer speak: We need access to a “remote” Gumball Machine object, one that “lives” in a different JavaVM, or address space.• To do this, we’ll use a technology built into Java, called RMI, short for Remote Method InvocationGumballMonitorProxyGumballMachineMachine BoundaryThe Gumball Monitor talks to the Proxy object, thinking that its a Gumball Machine object. The Proxy communicates with the “real” Gumball Machine, and returns any results back to the monitor.8Wednesday, December 5, 2007Approach• Quick Introduction to RMI• Change Gumball Machine so that it becomes a “remote service”• Create a Proxy object that can talk to this “remote service” while looking like a local Gumball Machine to the Gumball Monitor class9Wednesday, December 5, 2007Remote Method Invocation (I)ClientClientHelperMachine BoundaryServiceServiceHelperRMI creates “helper” objects that live on the client and service sides of a remote transaction. The client helper acts as a proxy for the remote service and sends method call information to the service helper. The service helper invokes the requested method on the service and returns the results.10Wednesday, December 5, 2007Remote Method Invocation (II)• RMI provides tools to automate the creation of the “helper” objects• The client helper is often called the “stub”• since none of the service methods are actually implemented• The service helper is often called the “skeleton”• since it often has methods that need to be filled in by the developer to hook it up to the actual service object• This architecture is common to many distributed computing frameworks• These architectures do a lot to make the distributed nature of these method calls hidden from the client object… its not possible to entirely hide this process however, and the client does need to be prepared for exceptions that wouldn’t normally occur when invoking methods on a local object11Wednesday, December 5, 2007Remote Method Invocation (III)• Step One: Make a remote interface• The interface defines the methods that the remote service provides to the client; both the client helper (stub) and the service implement this interface• Step Two: Make a remote implementation• The actual service object, in this case our Gumball Machine• Step Three: Generate the stubs and skeletons• Using a tool that ships with the Java SDK: rmic• Step Four: Start the RMI registry• So client


View Full Document

CU-Boulder CSCI 6448 - Proxy Pattern

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Proxy Pattern
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Proxy Pattern and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Proxy Pattern 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?