Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proxy Pattern Ralph Rodkey Josh Voils. Proxy Intent  Metsker and GoF:  To provide a surrogate, or placeholder, for another object to control access.

Similar presentations


Presentation on theme: "Proxy Pattern Ralph Rodkey Josh Voils. Proxy Intent  Metsker and GoF:  To provide a surrogate, or placeholder, for another object to control access."— Presentation transcript:

1 Proxy Pattern Ralph Rodkey Josh Voils

2 Proxy Intent  Metsker and GoF:  To provide a surrogate, or placeholder, for another object to control access to it

3 Proxy Uses  Remote proxy – provides a local representative for an object in a different address space  Virtual proxy – creates expensive objects on demand  Protection proxy – controls access to the original object

4 Proxy Uses – Cont.  Smart reference – A replacement for a bare pointer that performs additional actions when an object is accessed  Tracks the number of references to an object so it can be freed automatically when unneeded  Load a persistent object into memory when it’s first referenced  Mutexing the real object to ensure synchronicity

5 The ImageProxy example  Metsker and the GoF both use the ImageProxy example  An example of a Virtual Proxy

6 What the ImageProxy does  If we were to implement a picture browser, loading all of the images at runtime would incur a large memory cost  Solution: Use a small proxy object to be a placeholder, and let it load the actual image when requested

7  We’ll use ImageIcons and Jlabels to display images ImageIcon i = new ImageIcon("Fest.jpg"); JLabel l = new JLabel(i);

8

9 import java.awt.*; import javax.swing.*; public class ImageIconLoader extends ImageIcon implements Runnable { static final ImageIcon ABSENT = new ImageIcon("absent.jpg"); static final ImageIcon LOADING = new ImageIcon("loading.jpg"); protected String filename; protected JFrame callbackFrame; public ImageIconLoader(String filename) { super(ABSENT.getImage()); this.filename = filename; } public void load(JFrame callbackFrame) { // challenge! } public void run() { // challenge! }

10 Challenge!  Implement:  int getIconHeight()  int getIconWidth()  void paintIcon(Component c, Graphics h, int x, int y)

11 Solution public void load(JFrame callbackFrame) { this.callbackFrame = callbackFrame; setImage(LOADING.getImage()); callbackFrame.repaint(); new Thread(this).start(); } public void run() { setImage(new ImageIcon(filename).getImage()); callbackFrame.pack(); }

12 Questions


Download ppt "Proxy Pattern Ralph Rodkey Josh Voils. Proxy Intent  Metsker and GoF:  To provide a surrogate, or placeholder, for another object to control access."

Similar presentations


Ads by Google