动态代理

动态代理是在实现阶段不用关心代理谁,而在运行阶段蔡指定代理哪一个对象。

动态代理类:

public class GamePlayIH implements InvocationHandler{

Class cls=null;

Object obj=null;

public GamePlayIH(Object _obj){

this.obj=_obj;

}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{

Object result=method.invoke(this.obj,args);

return result;

}

}

 

场景类:

pubilc class Client{

public static void main(String[] args) throws Throwable{

IGamePlayer player=new PlayerCoCo();

InvocationHandler handler=new GamePlayerIH(player);

ClassLoader cl =player.getClass().getClassLoader();

IGamePlayer proxy=(IGamePlayer)Proxy.newProxyInstance(cl,new Class[]{IGamePlayer.class},handler);

proxy.login("zhangsan","pwd");                                                                                                                                    

}

}

posted @ 2017-10-20 17:10  海东青Lo  阅读(118)  评论(0)    收藏  举报