![Page 1: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/1.jpg)
Tecniche di Progettazione:
Design Patterns
GoF: Proxy
Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.1
![Page 2: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/2.jpg)
Revisit the Gumball machine example
� The same example covered in the State pattern
� Now we want to add some monitor to a collection of Gumball machines
![Page 3: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/3.jpg)
Gumball Class
![Page 4: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/4.jpg)
Gumball Monitor
![Page 5: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/5.jpg)
Role of the remote Proxy
![Page 6: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/6.jpg)
RMI Detour in looking at Proxy Pattern
![Page 7: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/7.jpg)
Remote Methods
![Page 8: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/8.jpg)
How the method call happens
Client calls method
![Page 9: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/9.jpg)
Client Helper forwards to service helper
![Page 10: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/10.jpg)
Service helper calls the real object
![Page 11: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/11.jpg)
Real object returns result
![Page 12: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/12.jpg)
Service helper forwards result to client
helper
![Page 13: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/13.jpg)
Client helper returns result to client
![Page 14: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/14.jpg)
![Page 15: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/15.jpg)
Steps in using Java RMI
![Page 16: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/16.jpg)
Additional steps
![Page 17: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/17.jpg)
STEP 1
Remote Interface
![Page 18: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/18.jpg)
STEP 1
Remote Interface
![Page 19: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/19.jpg)
STEP 2
Remote Implementation
![Page 20: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/20.jpg)
STEP 2
Remote Implementation
![Page 21: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/21.jpg)
STEP 3
Create Stubs &
Skeletons
![Page 22: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/22.jpg)
![Page 23: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/23.jpg)
![Page 24: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/24.jpg)
![Page 25: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/25.jpg)
Client talks to the stub
![Page 26: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/26.jpg)
Hooking up client and server objects
![Page 27: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/27.jpg)
![Page 28: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/28.jpg)
![Page 29: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/29.jpg)
Back to Gumball machine problem
![Page 30: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/30.jpg)
Gumball Machine remote interface
� import java.rmi.*;
�
� public interface GumballMachineRemote extends Remote {
� public int getCount() throws RemoteException;� public int getCount() throws RemoteException;
� public String getLocation() throws RemoteException;
� public State getState() throws RemoteException;
� }
![Page 31: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/31.jpg)
State interface extends Serializable
� import java.io.*;
�
� public interface State extends Serializable {
� public void insertQuarter();
� public void ejectQuarter();� public void ejectQuarter();
� public void turnCrank();
� public void dispense();
� }
![Page 32: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/32.jpg)
Use of keyword “transient”
public class NoQuarterState implements State {
transient GumballMachine gumballMachine;
public NoQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
public void insertQuarter() {
System.out.println("You inserted a quarter");
gumballMachine.setState(gumballMachine.getHasQuarterState());
}
// other methods
} The use of transient to ensure that the serialization does not involve this object as well.
![Page 33: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/33.jpg)
Proxy Pattern defined
The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.
The proxy pattern is used to create a The proxy pattern is used to create a representative object that controls access to another object, which may be remote, expensive to create or in need of securing.
![Page 34: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/34.jpg)
Proxy Class Diagram
![Page 35: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/35.jpg)
![Page 36: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/36.jpg)
Making the call
![Page 37: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/37.jpg)
![Page 38: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/38.jpg)
Remote Proxy
![Page 39: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/39.jpg)
Virtual Proxy
![Page 40: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/40.jpg)
Playing CD Covers
![Page 41: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/41.jpg)
Playing CD Cover Proxy
![Page 42: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/42.jpg)
ImageProxy process
![Page 43: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/43.jpg)
ImageProxy process
![Page 44: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/44.jpg)
![Page 45: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/45.jpg)
class ImageProxy implements Icon {
ImageIcon imageIcon;
URL imageURL;
Thread retrievalThread;
boolean retrieving = false;
public ImageProxy(URL url) { imageURL = url; }
public int getIconWidth() {
if (imageIcon != null) return imageIcon.getIconWidth();
else return 800; }
public int getIconHeight() {
if (imageIcon != null)return imageIcon.getIconHeight();
else return 600;}
public void paintIcon(final Component c, Graphics g, int x, int y) {
if (imageIcon != null) imageIcon.paintIcon(c, g, x, y);if (imageIcon != null) imageIcon.paintIcon(c, g, x, y);
else{ g.drawString("Loading CD cover, please wait...", x+300, y+190);
if (!retrieving) {
retrieving = true;
retrievalThread = new Thread(new Runnable() {
public void run() {
try {
imageIcon = new ImageIcon(imageURL, "CD Cover");
c.repaint();
} catch (Exception e) { e.printStackTrace();}
}
});
retrievalThread.start();
}
}
}
}
Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.45
![Page 46: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/46.jpg)
Using Java API’s Proxy to create a
protection proxy
<<interface>>Subject
request()
<<interface>>InvocationHandler
invoke()
RealSubject
request()
Proxy
request()
InvocationHandler
invoke()
![Page 47: Tecniche di Progettazione: Design Patternsdidawiki.cli.di.unipi.it/lib/exe/fetch.php/magistr... · Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for](https://reader034.vdocuments.site/reader034/viewer/2022051916/6007fc36cd5e4706884615cc/html5/thumbnails/47.jpg)
The proxy zoo
� Firewall proxy
� Smart Reference proxy
� E.g. counts the number of references
� Caching proxy
� Synchronization Proxy� Synchronization Proxy
� Complexity hiding Proxy
� Similar to façade pattern, it also controls accesses
� Copy-on-write Proxy
Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.47