⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleproxydemo21.java

📁 JAVA编程思想第四版英文原版习题答案. pdf原版的
💻 JAVA
字号:
// typeinfo/SimpleProxyDemo21.java
// TIJ4 Chapter Typeinfo, Exercise 21, page 598
// Modify SimpleProxyDemo.java so that it measures method-call times.
import static net.mindview.util.Print.*;
import java.util.*;

interface Interface {
	void doSomething();
	void somethingElse(String arg);
}

class RealObject implements Interface {
	public void doSomething() { print("doSomething"); }
	public void somethingElse(String arg) {
		print("somethingElse " + arg);
	}
}

class SimpleProxy implements Interface {
	private Interface proxied;
	private static int doCount = 0;
	private static int sECount = 0;
	public SimpleProxy(Interface proxied) {
		this.proxied = proxied;
	}
	public void doSomething() { 
		long timeIn = new Date().getTime();
		print("Time called doSomething() " + doCount + ": " + timeIn + " msecs");
		print("on " + new Date());
		doCount++;
		proxied.doSomething();
		print("Call-return time = " + ((new Date().getTime()) - timeIn) + " msecs");
	}
	public void somethingElse(String arg) {
		long timeIn = new Date().getTime();
		print("Time called somethingElse() " + sECount + ": " + timeIn + " msecs");
		print("on " + new Date());
		sECount++;
		proxied.somethingElse(arg);
		print("Call-return time = " + ((new Date().getTime()) - timeIn) + " msecs");
	}
}

class SimpleProxyDemo21 {
	public static void consumer(Interface iface) {
		iface.doSomething();
		iface.somethingElse("bonobo");
	}
	public static void main(String[] args) {
		consumer(new RealObject());
		print();
		consumer(new SimpleProxy(new RealObject()));
		print();
		consumer(new SimpleProxy(new RealObject()));
		print();
		consumer(new SimpleProxy(new RealObject()));					
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -