设计模式之禅-代理模式2
来源:互联网 发布:天津市软件协会 编辑:程序博客网 时间:2024/06/05 09:21
定义:自己写代理类的方式就是静态代理
承接上篇文章
类图中多了InvicationHandler,GamePlayerIH
作用是产生一个对象的代理对象
/** * 动态代理类 * 原理不清楚 */public class DynamicProxy implements InvocationHandler{ //被代理者 private Class cls; //被代理者的实例 private Object obj; //我要代理谁 public DynamicProxy(Object obj) { this.obj = obj; } //调用被代理的方法 @Override public Object invoke(Object o, Method method, Object[] objects) throws Throwable { return method.invoke(obj,objects); }}
/** * 动态代理场景类 * 1 new被代理的对象 * 2 new InvocationHandler * 3 创建代理对象 * 4 调用方法 * 具体原理不清楚 */public class DynamicClient extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); IGamePlayer gamePlayer = new GamePlayer("shj"); InvocationHandler handler = new DynamicProxy(gamePlayer); IGamePlayer proxy = (IGamePlayer) Proxy.newProxyInstance(gamePlayer.getClass().getClassLoader(),gamePlayer.getClass().getInterfaces(),handler); proxy.login("shj","ddd"); proxy.killBoss(); proxy.upgrade(); }}
阅读全文
0 0
- 设计模式之禅-代理模式2
- 代理模式 【设计模式之禅作者】
- 设计模式之禅笔记-代理模式
- 设计模式之2---代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之-代理模式
- 设计模式之 代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之代理模式
- 设计模式之-----代理模式
- Retrofit 简单应用
- java利用websocket搭建即时聊天系统
- 9. 总结及Mac安装Mayavi经验
- leetcode 24. Swap Nodes in Pairs
- css组合选择器、伪选择器
- 设计模式之禅-代理模式2
- Find them, Catch them
- 开源中国码云使用
- 手动获取截图和uix文件到 UI Automator Viewer 解析
- 安卓 发布平台 汇总
- 博弈—SG
- NOIP 提高组 2004
- Hibernate07_映射关系-MangToOne
- PL0编译器扩展