描述
Figure
下面的例子說明了如何在應用程序中利用計數代理
例子
讓我們設計一個Order類
Figure
public interface OrderIF {
public Vector getAllOrders();
}
作為getAllOrders方法實現的一部分
public class Order implements OrderIF {
public Vector getAllOrders() {
FileUtil fileUtil = new FileUtil();
Vector v = fileUtil
return v;
}
}
這個附加的功能可以設計一個單獨的OrderProxy類來實現
Figure
public class OrderProxy implements OrderIF {
private int counter =
public Vector getAllOrders() {
Order order = new Order();
counter++;
long t
Vector v = order
long t
long timeDiff = t
String msg =
//log the message
FileUtil fileUtil = new FileUtil();
fileUtil
return v;
}
}
客戶對象MainApp就想調用真實對象Order一樣調用OrderProxy對象上的getAllOrders()方法
public class MainApp {
public static void main(String[] args) {
OrderIF order = new OrderProxy();
Vector v = order
v = order
v = order
v = order
}
}
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27330.html