代理模式――Head First Design Patterns(一)

2014-11-24 03:13:55 · 作者: · 浏览: 2

定义:为对象提供一个代理以控制其它对象对它的访问

使用场景: 1)远程代理:访问远程对象 2)虚拟代理:根据需要创建开销大的对象 3)保护代理:控制对原对象的访问

类图:

\

代码样例:

package headfirst.proxy.virtualproxy;

import java.awt.*;
import javax.swing.*;

class ImageComponent extends JComponent {
	private Icon icon;

	public ImageComponent(Icon icon) {
		this.icon = icon;
	}

	public void setIcon(Icon icon) {
		this.icon = icon;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		int w = icon.getIconWidth();
		int h = icon.getIconHeight();
		int x = (800 - w)/2;
		int y = (600 - h)/2;
		icon.paintIcon(this, g, x, y);
	}
}


package headfirst.proxy.virtualproxy;

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ImageProxy implements Icon {
	ImageIcon imageIcon;
	URL imageURL;
	Thread retrieva lThread;
	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);
		} else {
			g.drawString("Loading CD cover, please wait...", x+300, y+190);
			if (!retrieving) {
				retrieving = true;

				retrieva lThread = new Thread(new Runnable() {
					public void run() {
						try {
							imageIcon = new ImageIcon(imageURL, "CD Cover");
							c.repaint();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
				retrieva lThread.start();
			}
		}
	}
}


package headfirst.proxy.virtualproxy;

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class ImageProxyTestDrive {
	ImageComponent imageComponent;
	JFrame frame = new JFrame("CD Cover Viewer");
    JMenuBar menuBar;
    JMenu menu;
	Hashtable cds = new Hashtable();
 
	public static void main (String[] args) throws Exception {
		ImageProxyTestDrive testDrive = new ImageProxyTestDrive();
	}
 
	public ImageProxyTestDrive() throws Exception{
		cds.put("Ambient: Music for Airports","https://www.cppentry.com/upload_files/article/76/1_vssgu__.jpg");
        cds.put("Buddha Bar","https://www.cppentry.com/upload_files/article/76/1_tuxdz__.jpg");
        cds.put("Ima","https://www.cppentry.com/upload_files/article/76/1_wtmft__.jpg");
        cds.put("Karma","https://www.cppentry.com/upload_files/article/76/1_a2wps__.gif");
        cds.put("MCMXC A.D.","https://www.cppentry.com/upload_files/article/76/1_kl6j2__.jpg");
        cds.put("Northern Exposure","https://www.cppentry.com/upload_files/article/76/1_pyqak__.jpg");
        cds.put("Selected Ambient Works, Vol. 2","https://www.cppentry.com/upload_files/article/76/1_w1peo__.jpg");

		URL initialURL = new URL((String)cds.get("Selected Ambient Works, Vol. 2"));
		menuBar = new JMenuBar();
		menu = new JMenu("Favorite CDs");
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);

		for(Enumeration e = cds.keys(); e.hasMoreElements();) {
			String name = (String)e.nextElement();
        	JMenuItem menuItem = new JMenuItem(name);
        	menu.add(menuItem); 
        	menuItem.addActionListener(new ActionListener() {
          		  public void actionPe