设计模式读书笔记-1代理模式Proxy(2)

[b]1.虚拟代理与状态模式相结合[/b]
State

package headfirst.proxy.virtualproxy;
import java.awt.Component;
import java.awt.Graphics;
public interface State {
public int getIconWidth();
public int getIconHeight();
public void paintIcon(final Component c, Graphics g, int x, int y);
}


ImageNotLoadedState

package headfirst.proxy.virtualproxy;

import java.awt.Component;
import java.awt.Graphics;
import java.net.URL;

import javax.swing.ImageIcon;

public class ImageNotLoadedState implements State {

private URL imageURL;
private ImageIcon imageIcon;
public ImageIcon getImageIcon() {
return imageIcon;
}

public void setImageIcon(ImageIcon imageIcon) {
this.imageIcon = imageIcon;
}

private boolean retrieving = false;
private Thread retrievalThread;

private ImageProxyWithState imageProxy;

public ImageNotLoadedState(ImageProxyWithState imageProxy, URL url) {
this.imageProxy = imageProxy;
this.imageURL = url;
}

public int getIconWidth() {
return 800;
}

public int getIconHeight() {
return 600;
}

public void paintIcon(final Component c, Graphics g, int x, int y) {
g.drawString("Loading CD cover, please wait...", x+300, y+190);
if (!retrieving) {
retrieving = true;

retrievalThread = new Thread(new Runnable() {
public void run() {
try {
imageIcon = new ImageIcon(imageURL, "CD Cover");
imageProxy.setCurrentState(imageProxy.getImageLoadedState());
((ImageLoadedState)imageProxy.getImageLoadedState()).setImageIcon(
((ImageNotLoadedState)imageProxy.getImageNotLoadedState()).getImageIcon());
c.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
});
retrievalThread.start();
}
}

}



ImageLoadedState

package headfirst.proxy.virtualproxy;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.ImageIcon;

public class ImageLoadedState implements State {

private ImageIcon imageIcon;

public ImageIcon getImageIcon() {
return imageIcon;
}

public void setImageIcon(ImageIcon imageIcon) {
this.imageIcon = imageIcon;
}

public int getIconWidth() {
return imageIcon.getIconWidth();
}

public int getIconHeight() {
return imageIcon.getIconHeight();
}

public void paintIcon(Component c, Graphics g, int x, int y) {
imageIcon.paintIcon(c, g, x, y);
}

}



ImageProxyWithState

package headfirst.proxy.virtualproxy;

import java.awt.Component;
import java.awt.Graphics;
import java.net.URL;

import javax.swing.Icon;

class ImageProxyWithState implements Icon {
State currentState;
State imageNotLoadedState;
State imageLoadedState;

public ImageProxyWithState(URL url) {
imageNotLoadedState = new ImageNotLoadedState(this, url);
imageLoadedState = new ImageLoadedState();
currentState = imageNotLoadedState;
}

public State getCurrentState() {
return currentState;
}

public void setCurrentState(State currentState) {
this.currentState = currentState;
}

public State getImageNotLoadedState() {
return imageNotLoadedState;
}

public void setImageNotLoadedState(State imageNotLoadedState) {
this.imageNotLoadedState = imageNotLoadedState;
}

public State getImageLoadedState() {
return imageLoadedState;
}

public void setImageLoadedState(State imageLoadedState) {
this.imageLoadedState = imageLoadedState;
}

public int getIconWidth() {
return currentState.getIconWidth();
}

public int getIconHeight() {
return currentState.getIconHeight();
}

public void paintIcon(final Component c, Graphics g, int x, int y) {
currentState.paintIcon(c, g, x, y);
}
}



[b]2.Java动态代理实现机制及扩展[/b]
[url]http://www.ibm.com/developerworks/cn/java/j-lo-proxy1/[/url]
[url]http://www.ibm.com/developerworks/cn/java/j-lo-proxy2/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值