结构型模式:结构型对象模式不是对接口或实现进行组合的.而是描述了如何对一些对象进行组合,从而实现新功能的一些方法.
proxy:为其它对象提供一种代理,以控制对这个对象的访问.
1、远程代理
2、虚代理
3、保护代理
4、智能指引
代码:
package structure.proxy;
/**
* A graphic interface that proxy and real object should implement
* so a proxy can proxy several real object
*/
public interface Graphic {
public abstract void Draw();
}
package structure.proxy;
/**
* A Image
*/
import java.awt.MediaTracker;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
public class ImageA implements Graphic {
public ImageA(String _fileName) {
fileName = _fileName;
}
public void Draw() {
}
public MediaTracker tracker;
private int height, width;
private String fileName;
}
package structure.proxy;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//swing classes
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.event.*;
public class JxFrame extends JFrame
{
public JxFrame(String title)
{
super(title);
setCloseClick();
setLF();
}
private void setCloseClick()
{
//create window listener to respond to window close click
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);}
});
}
//------------------------------------------
private void setLF()
{
// Force SwingApp to come up in the System L&F
String laf = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
}
catch (UnsupportedLookAndFeelException exc)
{System.err.println("Warning: UnsupportedLookAndFeel: " + laf);}
catch (Exception exc) {System.err.println("Error loading " + laf + ": " + exc);
}
}
}
package structure.proxy;
import java.awt.event.*;
import javax.swing.*;
public class PageProxy extends JPanel {
public PageProxy() {
label = new JLabel("Loading web page !!!!!");
this.add(label);
}
public JLabel label;
}
package structure.proxy;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import java.awt.*;
public class ProxyDisplay extends JxFrame
{
public ProxyDisplay()
{
super("Display proxied image");
JPanel p = new JPanel();
getContentPane().add(p);
p.setLayout(new BorderLayout());
ImageProxy image = new ImageProxy("d://JavaProject//Proxy//src//elliott.jpg", 321, 271);
//ImageProxy image = new ImageProxy("c://winnt//web//Wallpaper//Windows 2000.jpg", 321, 271);
p.add("Center", image);
p.add("North", new Label(" "));
p.add("West", new Label(" "));
setSize(370, 350);
setVisible(true);
}
//------------------------------------
static public void main(String[] argv)
{
new ProxyDisplay();
}
}
//==================================
class ImageProxy extends JPanel
implements Runnable
{
int height, width;
MediaTracker tracker;
Image img;
JFrame frame;
Thread imageCheck; //to monitor loading
//------------------------------------
public ImageProxy(String filename, int w, int h)
{
height = h;
width = w;
tracker = new MediaTracker(this);
img = Toolkit.getDefaultToolkit().getImage(filename);
tracker.addImage(img, 0); //watch for image loading
//imageCheck = new Thread(this);
//imageCheck.start(); //start 2nd thread monitor
//this begins actual image loading
try{
tracker.waitForID(0,1);
}
catch(InterruptedException e){}
imageCheck = new Thread(this);
imageCheck.start(); //start 2nd thread monitor
}
//------------------------------------
public void paint(Graphics g)
{
if (tracker.checkID(0))
{
height = img.getHeight(frame); //get height
width = img.getWidth(frame); //and width
g.setColor(Color.lightGray); //erase box
g.fillRect(0,0, width+5, height+5);
g.drawImage(img, 0, 0, this); //draw loaded image
}
else
{
//draw box outlining image if not loaded yet
g.setColor(Color.red);
g.drawRect(1, 1, width+10, height+10);
}
}
//------------------------------------
public Dimension getPreferredSize()
{
return new Dimension(width, height);
}
//public int getWidth() {return width;}
//public int getHeight(){return height;}
//------------------------------------
public void run()
{
//this thread monitors image loading
//and repaints when done
//the 1000 msec is artifically long
//to allow demo to display with delay
try{
Thread.sleep(1000);
while(! tracker.checkID(0))
Thread.sleep(1000);
}
catch(Exception e){}
repaint();
}
}
package structure.proxy;
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("Proxy test client");
JLabel label = new JLabel("Loading web page ......");
frame.getContentPane().add(label);
PageProxy pageProxy = new PageProxy();
frame.getContentPane().add(pageProxy);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
//frame.pack();
frame.setVisible(true);
pageProxy.label.setText("aaa");
}
}