作者kingdeath (新的開始)
看板java
標題[問題] InvocationHandler作用?
時間Wed Apr 9 21:20:06 2014
各位先進好,今天在看動態代理時一直搞不清楚
InvocationHandler的用途到底是什麼。
為何我需要寫一個class去implements InvocationHandler,
然後再用Proxy.newProxyInstance()的方式呼叫實際Interface的方法。
如果我直接像是用以下的方式:
class MyObject implements MyIntf{
private MyIntf intf;
public MyObject(MyIntf intf){
this.intf = intf;
}
@Override
public void test(){
intf.test();
}
}
這樣的寫法好像差不多,而且code還比較少,不用去管啥InvocationHandler。
那到底動態代理的意義是何在呢?謝謝解答一下。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.137.227
※ 文章網址: http://www.ptt.cc/bbs/java/M.1397049610.A.E6B.html
→ ms10058:It can implement an interface using Refection 04/11 17:29
→ ms10058:例如:reflect你的callback/listener class. 04/11 17:32