1 / 103

Unit 6 Proxy

Unit 6 Proxy. Summary prepared by Kirk Scott. Design Patterns in Java Chapter 11 Proxy. Summary prepared by Kirk Scott. The Downy Woodpecker ( Picoides pubescens ) is a species of woodpecker , the smallest in North America . The Introduction Before the Introduction.

bebe
Download Presentation

Unit 6 Proxy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 6Proxy Summary prepared by Kirk Scott

  2. Design Patterns in JavaChapter 11Proxy Summary prepared by Kirk Scott

  3. The Downy Woodpecker (Picoidespubescens) is a species of woodpecker, the smallest in North America.

  4. 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

  5. 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

  6. 4. The last section of the chapter concerns dynamic proxies • This is also a useful application area for the design pattern • However, this is advanced in nature and it is not necessarily a frequently occurring application • Like the previous section, this will only be covered in passing

  7. 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, in order to achieve a particular goal, responsibility for a given class is apportioned into another class

  8. 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

  9. 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 • In other words, the proxy is responsible for providing a public interface for/receiving calls that are ultimately aimed at another, base class

  10. 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.

  11. 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”, selectively, or “with modifications” forwarding the requests to the underlying object

  12. 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

  13. 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

  14. 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

  15. The user sees the temporary image quickly and can proceed using the application • The loading time of the real image doesn’t affect the immediate response time needed to show the temporary image

  16. Something else to consider: • If the real image is important, can you really proceed to use the application without it? • If it’s not important, does it really matter what image you’re seeing on the screen? • Consider splash screens in Web pages • Basically, you’d like to show users one thing • In the meantime, showing them something else is better than showing them nothing at all

  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

  18. 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);

  19. 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 makes use of the proxy • And the proxy calls the base image/icon object • The following sequence diagram illustrates the idea

  20. 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

  21. If you haven’t had CS 320 before or threads in some other setting, don’t worry • Although part of this example, it is not central to the structure and intent of the design pattern

  22. If you have encountered threads before, you might consider this thought: • Multi-threading in a single processor environment may not lead to any real time savings • The user will see the temporary icon immediately, but it still won’t be possible to do anything until the thread loading the real image finishes running • In other words, this would be purely cosmetic (which might be desirable in its own right)

  23. 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 discussion of the structure of the pattern in general • Following that, the example will be covered in detail, with code

  24. Example UML

  25. 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 proxy class has an instance variable which is typed ImageIcon, the type of its superclass

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

  27. The intent is to create a proxy • The structure used to do so is a class with a reference to an instance of its superclass • This may seem a little weird, but it’s certainly possible • We’re going to see the same structure in other patterns later on

  28. Even though the book is interested in intent, it clearly identifies this pattern by structure • As we will see, it ultimately finds the structure troublesome • It will eventually abandon this structure in favor of a simpler structure which it will call a “proxy in spirit”

  29. Continuing the Discussion of the Example • The ImageIcon instance variable in the ImageIconProxy class is a reference to the icon which the proxy is currently displaying • Initially it is a reference to the temporary image • Later, it will refer to the real image icon when that one is finally being displayed

  30. The ImageIconProxy class implements Runnable, so it has a run() method • As mentioned already, that is part of the example, and it will continue to be shown • It is incidental, not fundamental to the pattern

  31. 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 • Note that in reality these are constants, not variables

  32. 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 • The frame reference is used to refresh what is shown on the screen when the ImageIconProxy variable takes on a new value

  33. The ImageIconProxy class inherits the public interface of its superclass, ImageIcon • 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

  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 the containing frame as a parameter • When called, the load() method switches the current image to “Loading” • It then repaints the frame

  35. After that, the load() method starts a new thread for loading the actual image • The run() method of the thread does the loading of the actual image • 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 beginning on the following overhead.

  36. 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 JFramecallbackFrame;

  37. public ImageIconProxy(String filename) • { • super(ABSENT.getImage()); • this.filename = filename; • } • public void load(JFramecallbackFrame) • { • this.callbackFrame = callbackFrame; • current = LOADING; • callbackFrame.repaint(); • new Thread(this).start(); • } • public void run() • { • current = new ImageIcon(ClassLoader.getSystemResource(filename)); • callbackFrame.pack(); • }

  38. public intgetIconHeight() • { • return current.getIconHeight(); • } • public intgetIconWidth() • { • return current.getIconWidth(); • } • public synchronized void paintIcon(Component c, Graphics g, int x, int y) • { • current.paintIcon(c, g, x, y); • } • }

  39. The Structure of a Proxy in Its Most Basic Form • The basic structure of the proxy pattern, as presented in the book, was mentioned earlier • It is repeated again here in some depth for reference purposes • The book is ready to critique the pattern and suggest an alternative • It’s helpful to see exactly what it’s talking about

  40. 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

  41. 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

  42. 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

  43. In the future we will see another design pattern, Decorator, where a subclass has a reference to an instance of its superclass • Decorator is definitely useful, so that structure is not necessarily bad • However, we will see now that the book is backing away from its use in Proxy because it may be less than ideal

More Related