Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary...

92
Unit 6 Proxy Summary prepared by Kirk Scott 1

Transcript of Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary...

Page 1: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

1

Unit 6Proxy

Summary prepared by Kirk Scott

Page 2: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

2

Design Patterns in JavaChapter 11

Proxy

Summary prepared by Kirk Scott

Page 3: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

3

The Introduction Before the Introduction

• The chapter in the book is broken into four different parts

• 1. An illustration of the design pattern for image loading– This example shows the accepted use of classes and

relationships between them for implementing the proxy pattern

• 2. A refactoring of the original design– The idea is that the same functional goal is achieved

but by means which might be better

Page 4: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

4

• 3. The book then considers the use of what it calls remote proxies– Proxies can definitely be useful in this

environment– However, writing code that is distributed on

different hardware platforms is beyond the scope of this course

– Therefore, this section of the chapter will only be covered in passing

Page 5: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

5

• 4. The last section of the chapter concerns dynamic proxies– This is also a useful application area for the design

pattern– However, this is very advanced in nature and it is

not necessarily a frequently occurring application– Like the previous section, this will only be covered

in passing

Page 6: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

6

Beginning of Book Material

• Responsibility in a design means that every class implements the code to support itself

• Recall how the builder pattern relaxed this• Some of the construction logic was offloaded• Proxy is another design pattern where the

customary treatment of responsibility isn’t ideal or might be improved upon

Page 7: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

7

• In simple terms, a proxy is a substitute that you give to a client when it requests a base object

• There are 3 cases where this might be desirable:• 1. When the base object might take too long to

load• 2. When the base object is on another computer• 3. When you would like to intercept method calls

to a base object so that you can wrap the underlying call to that object with code of your own

Page 8: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

8

• In those cases a software design might include a proxy class

• A proxy object stands in for a base object• Calls are made on the proxy object• The proxy then forwards those calls to the

base object

Page 9: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

9

Book Definition of Pattern

• Book definition:• The intent of the Proxy pattern is to control

access to an object by providing a surrogate, or placeholder, for it.

Page 10: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

10

A Classic Example: Image Proxies

• A proxy is a substitute or stand-in for a base object

• As such, it is desirable for it to have a generic interface (set of public methods) that is nearly identical to the interface of the base object

• The proxy works by receiving the calls and “judiciously” or selectively forwarding the requests to the underlying object

Page 11: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

11

• The term “stand-in” has been used to describe a proxy

• A proxy might also be understood as a wrapper, depending on how it’s implemented

• Or it might be understood as a go-between• Because it stands between the client and the

base object, it might forward some calls unchanged, while other calls may have code added or modified in some other way

Page 12: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

12

• A classic use of proxies is to avoid the waiting time associated with loading images in applications

• Suppose the application contains graphical elements which hold the images

• A proxy for a graphical element could take the step of displaying a small, temporary image, while the real image is being loaded in the background

Page 13: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

13

• In other words, a load() call is issued to the proxy

• The proxy loads a temporary image and calls load() on the base object in the background

• The real load() may be set to run in a separate thread

Page 14: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

14

• The user sees the temporary image quickly and can proceed using the application

• The loading time of the real image doesn’t affect response time

• (In reality, time savings from multi-threading in a single processor environment may not be apparent.)

Page 15: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

15

Shortcomings of the Pattern

• The book admits up front that the following shortcomings may apply to a proxy design:

• The resulting design may be brittle or fragile• By this the authors mean that maintenance

and modification may be difficult• This is a bad sign for the application of a

design pattern

Page 16: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

16

• The difficulty arises directly from the fact that the proxy is supposed to duplicate the interface of the underlying object

• If that interface includes lots of methods, you have two choices

• Implement them all: This requires lots of work• Implement only some of them: This means that

the proxy can only handle a subset of calls that the base object can handle

Page 17: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

17

An Illustration of How the Pattern Might Be Used

• The next overhead illustrates the idea of a graphical placeholder in an application that is supposed to display a picture

• Initially an application would present the “Absent” icon

• After pressing the “Load” button, the “Loading…” icon would be displayed as a stand-in

• In the background, the loading of the real image would be started, and eventually the real image would be displayed

Page 18: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

18

Page 19: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

19

The Mechanics of the Example

• From the standpoint of mechanics, the book proposes displaying .jpg images as icons that are inserted into labels

• This is the basic syntax:• ImageIcon icon = new ImageIcon(“images/fest.jpg”);• JLabel label = new JLabel(icon);

Page 20: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

20

• The idea now is to give the JLabel a proxy in place of the real icon object

• Then– The application makes use of the label– The label calls the proxy– And the proxy calls the base image/icon object

• The following sequence diagram illustrates the idea

Page 21: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

21

Page 22: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

22

Threads in the Example

• In the previous descriptions, reference was made to “loading an image in the background”

• In this example this will mean having the proxy implement the Runnable interface so that the proxy can load the image in a separate thread

Page 23: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

23

• If you’ve had CS 320 before or have encountered threads in some other setting, this might be meaningful

• If not, don’t worry• Although central to this example, it is not

central to the structure and intent of the design pattern

Page 24: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

24

A UML Diagram for the Example

• A UML diagram showing the relationship between the ImageIcon and ImageIconProxy classes is given on the following overhead

• This will be followed by a detailed discussion of what the diagram shows

• That will be followed by further discussion of how the example works, and code for it

Page 25: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

25

Example UML

Page 26: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

26

Static Structure of the Pattern Example

• This is the structure of the proxy example:• There is a Java class, ImageIcon, which is the

graphical item which is displayed in a JLabel• The proxy, ImageIconProxy, is a subclass of the

ImageIcon class• The ImageIconProxy class implements

Runnable, so it has a run() method

Page 27: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

27

• The proxy class has an instance variable which is typed ImageIcon, the type of its superclass

• This is the icon which the proxy is currently displaying

• This is the instance variable which will refer to the real image icon when it is finally the one being displayed

Page 28: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

28

• The structure of the example can be summarized as shown in the simplified UML diagram on the following overhead

• The book is interested in intent, but it clearly identifies this pattern by structure

• As we will see, it ultimately finds the structure troublesome, and abandons it in favor of a different structure which it then calls a “proxy in spirit”

Page 29: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

29

Page 30: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

30

• The ImageIconProxy class has two other instance variables for images

• A static ImageIcon variable for the Absent icon• A static ImageIcon variable for the Loading

icon

Page 31: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

31

• The ImageIconProxy class also has an instance variable to hold the String path name of the real image to load

• And the ImageIconProxy class has an instance variable that holds a reference to the frame of the application that is using it

Page 32: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

32

• As a subclass of the ImageIcon class, the proxy class, ImageIconProxy, will have an interface that contains at a minimum the same methods as its superclass

• In other words, it inherits the interface• Potentially it could also implement other

methods

Page 33: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

33

• The ImageIconProxy class overrides a few of the many methods in the ImageIcon class

• It also has a load() method that triggers the loading of an image

Page 34: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

34

How It Works

• When an instance of the ImageIconProxy class is constructed, it takes in the String path name of an actual image to be loaded

• The load() method takes in a reference to a frame as a parameter

• When called, the load() method switches the current image to “Loading”

Page 35: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

35

• It then repaints the frame• After that, it starts a new thread for loading

the actual image• The run() method basically just does the

loading of the actual image

Page 36: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

36

• In the book, the code for the ImageIconProxy class is given in partial form, followed by a challenge to complete it

• Instead of doing a challenge, the complete code is shown here.

Page 37: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

37

ImageIconProxy Code• import java.awt.*;• import javax.swing.*;

• public class ImageIconProxy extends ImageIcon implements Runnable

• {• static final ImageIcon ABSENT = new

ImageIcon(ClassLoader.getSystemResource("images/absent.jpg"));• static final ImageIcon LOADING = new

ImageIcon(ClassLoader.getSystemResource("images/loading.jpg"));• ImageIcon current = ABSENT;• protected String filename;• protected JFrame callbackFrame;

Page 38: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

38

• public ImageIconProxy(String filename)• {• super(ABSENT.getImage());• this.filename = filename;• } • public void load(JFrame callbackFrame) • {• this.callbackFrame = callbackFrame;• current = LOADING;• callbackFrame.repaint();• new Thread(this).start();• }• public void run()• {• current = new

ImageIcon(ClassLoader.getSystemResource(filename));• callbackFrame.pack();• }

Page 39: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

39

• public int getIconHeight() • {• return current.getIconHeight();• }

• public int getIconWidth() • {• return current.getIconWidth();• }• public synchronized void paintIcon(Component c, Graphics

g, int x, int y) • {• current.paintIcon(c, g, x, y);• }• }

Page 40: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

40

The Structure of a Proxy in Its Most Basic Form

• At the most basic level, the ImageIconProxy class has an instance variable which is a reference to an ImageIcon, the current image

• The situation can be diagrammed as shown on the following overhead

Page 41: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

41

Page 42: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

42

• Recall that the ImageIconProxy class is a subclass of the ImageIcon class

• It also implements the Runnable interface• A more complete diagram, including those

elements, is shown on the following overhead• Note that implementing the Runnable

interface is incidental, but not necessarily fundamental to the pattern

Page 43: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

43

Page 44: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

44

• In summary, the structure of the pattern can be described in this way:

• A subclass has an instance variable that is a reference to the subclass’s superclass

• Or, the subclass wraps an instance of its superclass

• The functional result is that the subclass provides an extended interface for working with an instance of the superclass

Page 45: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

45

Problems with the Example Application of the Pattern

• Challenge 11.2• The ImageIconProxy class is not a well-

designed, reusable component. • Point out two problems with the design.

Page 46: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

46

• Solution 11.2• “Problems with the design include the following:• 1. Forwarding only a subset of calls to an

underlying ImageIcon object is dangerous.• The ImageIconProxy class inherits a dozen fields

and at least 25 methods from the ImageIcon class.• To be a true proxy, the ImageIconProxy object

needs to forward most or all of these calls.

Page 47: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

47

• Thorough forwarding would require many potentially erroneous methods, and this code would require maintenance as the ImageIcon class and its superclasses change over time.”

Page 48: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

48

• Comment mode on:• Note that when the authors say “erroneous”,

it is not entirely clear what they mean.• They may mean that due to the nature of the

subclass, the inherited versions of the methods will not typically apply

• That would mean having to override most or all of the methods of the superclass

Page 49: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

49

• Or by “erroneous” they may mean that trying to completely duplicate the interface would be error prone

• Their statement does make it clear that maintenance would be a problem if the methods of the superclass that are overridden are subject to change

Page 50: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

50

• They may also be suggesting that you might try to ease your work by not overriding all methods, relying on inheritance for “unimportant” methods, even if the inherited version doesn’t apply to the subclass

• Or they may be suggesting that you might supply a number of bogus implementations in the subclass

Page 51: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

51

• [Back to the problems identified by the book.]• “2. You might question whether the “Absent”

image and the desired image are in the right places in the design.

• It might make more sense to have the images passed in rather than making the class responsible for finding them.”

• [This seems like a relatively minor criticism. In their second, improved design the authors don’t really change this.]

Page 52: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

52

Applying a Pattern May Not Always Be a Good Idea

• In summary, the software design presented so far does include the Proxy design pattern

• However, the authors aren’t satisfied that the design that results from using this pattern is good

• Whenever you apply a pattern, it’s not sufficient to justify its use on the grounds that it’s a pattern

Page 53: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

53

• Its use has to lead to a better design• As a matter of fact, the authors’ critique

seems to be of the pattern itself, not just its application

• Whether good or not, the foregoing example is the clearest illustration of the structure of the Proxy design pattern that is presented by the book

Page 54: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

54

Image Proxies Reconsidered

• Preliminary comment mode on:• What the authors do next is quite simple• I’m not sure their explanation really makes it

clear what’s happening because it’s so simple• What they do is stop trying to make a proxy• Instead, they solve their design problem with

a subclass with no pretenses to being a proxy

Page 55: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

55

• The key point is this:• The subclass no longer contains a reference to

a superclass object• Surely this is a cleaner approach• The subclass is simple “a kind of” its

superclass• It is not a wrapper for an instance of its

superclass

Page 56: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

56

• With a proxy, you are either committed to reproducing the whole interface

• Or you decide to do a subset• With the subclass solution you rely on all of

the inherited methods of the superclass and override as needed

• Plus you may add selected methods as needed

Page 57: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

57

• What you want to accomplish with your new design is the same as with a proxy, but the task is clearer

• You’re back to the familiar problem of designing a subclass with special, useful characteristics that distinguish it from the superclass

Page 58: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

58

Specifics of the Book’s New Solution

• The book replaces the ImageIconProxy subclass of ImageIcon with a new subclass, LoadingImageIcon

• In addition to the inherited interface, the LoadingImageIcon class has two methods in it, load() and run()

• The ImageIconProxy class had a few more methods in it, including, for example paintIcon()

Page 59: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

59

• The superclass, ImageIcon, contains an instance of the Image class

• The ImageIcon class contains all the methods needed for manipulating the image

• The LoadingImageIcon class inherits these methods

• It doesn’t have to duplicate the interface• A UML diagram of the new solution is given on

the following overhead

Page 60: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

60

New Solution UML

Page 61: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

61

Code for the New Solution

• The code for the LoadImageIcon class follows• The basic idea behind load() and run() is

similar to before• However, the code is simpler because it relies

more directly on the ImageIcon class

Page 62: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

62

• Just like with the code for the previous example, the book gives partial code for the LoadingImageIcon class followed by a challenge to complete it

• Instead of doing a challenge, the complete code is given here.

Page 63: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

63

LoadingImageIcon Code• import javax.swing.ImageIcon;• import javax.swing.JFrame;• public class LoadingImageIcon extends ImageIcon implements

Runnable • {• static final ImageIcon ABSENT = new

ImageIcon(ClassLoader.getSystemResource("images/absent.jpg"));• static final ImageIcon LOADING = new

ImageIcon(ClassLoader.getSystemResource("images/loading.jpg"));• protected String filename;• protected JFrame callbackFrame;

• public LoadingImageIcon(String filename) • {• super(ABSENT.getImage());• this.filename = filename;• }

Page 64: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

64

• public void load(JFrame callbackFrame)• {• this.callbackFrame = callbackFrame;• setImage(LOADING.getImage());• callbackFrame.repaint();• new Thread(this).start();• }

• public void run()• {• setImage(new

ImageIcon(ClassLoader.getSystemResource(filename)).getImage());• callbackFrame.pack();• }• }

Page 65: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

65

Another Example

• The following example is given for one reason only:

• It is an attempt to illustrate what good an application of the proxy pattern might be.

• It emphatically doesn’t illustrate the structure and use of the pattern itself

• In other words, you won’t find in it a subclass containing a reference to a superclass object

Page 66: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

66

• On the other hand, you can identify the class in this example which implements the desired functionality

• That is, like the book’s second example, you can identify that part of the application which is a “proxy in spirit”

Page 67: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

67

• The book’s discussion started with the question of displaying images.

• However, when I tried the book’s code, everything went so quickly when loading a local image that you didn’t see the “Loading…” icon

• For all practical purposes, it wasn’t apparent that any kind of proxy was in use

Page 68: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

68

• This example makes use of Web pages—local and remote ones—so that there can be a perceptible delay in showing them

• The pages are displayed in an editor screen• The local ones display nicely• A remote Web page will probably display

strangely—but it will display, and display with a delay, which is the point

Page 69: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

69

• The set of screenshots on the following overheads shows what you see when using the application

Page 70: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

70

Page 71: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

71

Page 72: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

72

Page 73: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

73

Page 74: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

74

• A UML diagram for the application is given on the following overhead

• The JEditorPagePaneLoader class contains a load() method and a run() method

• It implements the proxy functionality• The application could be improved, but when

you run it you will find that it does serve the purpose of actually showing what happens on the screen

Page 75: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

75

Page 76: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

76

• The code for the application is given on the following overheads for reference.

• It is unlikely that it will be read in class.

Page 77: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

77

• import javax.swing.JEditorPane;• import java.io.IOException;• import java.awt.*;• import java.awt.event.*;• import javax.swing.*;• import java.lang.*;• import java.util.*;

• public class EditorPane• {• public static void main(String[] args)• {• EditorPaneFrame myframe = new EditorPaneFrame();• myframe.setVisible(true);• }• }

Page 78: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

78

• class EditorPaneFrame extends JFrame• {• private EditorPanePanel myEditorPanePanel;• private final int FRAMEW = 1000;• private final int FRAMEH = 500;

Page 79: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

79

• public EditorPaneFrame()• {• setTitle("EditorPane Frame");• setSize(FRAMEW, FRAMEH);• setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

• myEditorPanePanel = new EditorPanePanel();

• Container contentPane = getContentPane();• contentPane.add(myEditorPanePanel, "Center");

• JMenuBar menuBar = new JMenuBar();• setJMenuBar(menuBar);

• JMenu fileMenu = new JMenu("File");• menuBar.add(fileMenu);

• JMenuItem editorPaneItem = new JMenuItem("Load Editor Pane");• fileMenu.add(editorPaneItem);

• EditorPaneListener myEditorPaneListener = new EditorPaneListener();

• editorPaneItem.addActionListener(myEditorPaneListener);• }

Page 80: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

80

• private class EditorPaneListener implements ActionListener

• {• public void actionPerformed(ActionEvent event)• {• String localString = "";• localString =

JOptionPane.showInputDialog("Enter a URL");• myEditorPanePanel.setPage(localString);• }• }• }

Page 81: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

81

• class EditorPanePanel extends JPanel• {• private JEditorPanePageLoader mypane;

• public EditorPanePanel()• {• try• {• mypane = new JEditorPanePageLoader();• this.add(mypane);• }• catch(IOException ioe)• {• System.out.println("Caught an I/O exception when trying to

open URL.");• System.out.println(ioe);• }• }

• public void setPage(String stringURLIn)• {• mypane.loadPage(stringURLIn);• }• }

Page 82: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

82

• class JEditorPanePageLoader extends JEditorPane implements Runnable• {• private String stringURL;

• public JEditorPanePageLoader() throws IOException• {• super("http://math.uaa.alaska.edu/~afkas/initiallocalpage.htm");• }

• public void loadPage(String stringURLIn)• {• stringURL = stringURLIn;

• try• {• setPage("http://math.uaa.alaska.edu/~afkas/loadingpage.htm");• }• catch(IOException ioe)• {• System.out.println("Caught an I/O exception when trying to open

URL.");• System.out.println(ioe);• }

• new Thread(this).start();• }

Page 83: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

83

• public void run()• {• try• {• setPage(stringURL);• }• catch(IOException ioe)• {• System.out.println("Caught an I/O exception

when trying to open URL.");• System.out.println(ioe);• }• }

• }

Page 84: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

84

A UML Diagram for the Pattern from Lasater

• Lasater’s UML diagram is given on the next overhead.

• Using that author’s terminology, the RealSubject and the Proxy are both subclasses of a Subject class, and the Client has a reference to a subject

• In fact, under this plan, the client would have a superclass reference to a proxy object

Page 85: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

85

• The other part of the pattern is clearly shown:• The proxy has an instance variable that is of

the type RealSubject• Because of the presence of the Subject

superclass, the reference is to a sibling• In the previous discussion, the so-called

RealSubject was the superclass, so the reference was to a superclass

Page 86: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

86

Page 87: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

87

Remote Proxies

• As stated at the beginning of this set of overheads, code running on remote machines is beyond the scope of this course

• However, it is worth noting how the idea of a proxy might be useful

• The proxy runs on the local machine and local code calls it

• The proxy does what it can to satisfy the request locally while forwarding calls to the remote object

Page 88: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

88

Dynamic Proxies

• The full treatment of dynamic proxies, like remote proxies, is beyond the scope of this course

• As presented in the book, dynamic proxies rely on Java reflection, which is a large and relatively complicated topic

• The basic idea is that you can instrument code by wrapping a base object with a proxy, where the proxy method code contains instrumentation which surrounds the forwarding of calls to the methods of the base object

Page 89: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

89

Summary

• A proxy is a placeholder object that manages access to a target object

• A proxy can insulate clients from various aspects of target object code, like the time taken to load a graphics object

• A proxy can also wrap calls to the target’s methods, allowing the insertion of other code before and after

Page 90: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

90

• The Proxy design pattern is responsibility based

• A proxy assumes responsibility for the target when the client makes a call

• The very nature of the proxy leads to its weakness as a design pattern

• The proxy is tightly coupled with the target

Page 91: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

91

• A choice has to be made between trying to implement the whole interface of the target and implementing only part

• Implementing it all is a lot of work• Implementing part potentially means “gaps” in

what can be done with the proxy• Either way, the proxy is heavily dependent on

the target class and any changes made to it

Page 92: Unit 6 Proxy Summary prepared by Kirk Scott 1. Design Patterns in Java Chapter 11 Proxy Summary prepared by Kirk Scott 2.

92

The End