现在的位置: 首页 > 综合 > 正文

静态代理模式

2013年10月07日 ⁄ 综合 ⁄ 共 737字 ⁄ 字号 评论关闭
public interface Moveable {
	void move();
}
public class Tank implements Moveable{

	@Override
	public void move() {
		System.out.println("我是坦克,所向披靡,进攻中!");
		try {
			Thread.sleep(new Random().nextInt(1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
public class ProxyTankTime implements Moveable{
	private Moveable move;
	
	public ProxyTankTime(Moveable move){
		this.move=move;
	}
	
	@Override
	public void move(){
		long start=System.currentTimeMillis();
		System.out.println("坦克开始移动....");
		move.move();
		System.out.println("移动的时间:"+(System.currentTimeMillis()-start));
	}

}
public class Client {
	public static void main(String[] args) {
//		Moveable move=new Tank();
//		move.move();
		/**
		 * 运行结果:
		 * 我是坦克,所向披靡,进攻中!
		 */
		Moveable move=new ProxyTankTime(new Tank());
		move.move();
		/**
		 * 运行结果:
		 * 坦克开始移动....
		 * 我是坦克,所向披靡,进攻中!
		 * 移动的时间:515
		 */
	}
}

抱歉!评论已关闭.