simpleproxydemo21.java

来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 58 行

JAVA
58
字号
// 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 + =
减小字号Ctrl + -
显示快捷键?