zoukankan      html  css  js  c++  java
  • Java 代理模式

    代理模式

    代理模式 实现逻辑和实现的解耦

    代理模式 为了提供额外的的操作,插入用来代替实际对象的对象。这些操作通常涉及与实际对象通信,代理充当中间人的角色

    • 接口
    /**
     * 接口
     */
    public interface Interface {
        void doSomething();
        void somethingElse(String arg);
    }
    
    • 实际对象
    /**
     * 实际对象
     */
    public class RealObject implements Interface {
        public void doSomething() {
            System.out.println("doSomething");
        }
    
        public void somethingElse(String arg) {
            System.out.println("somethingElse" + arg);
        }
    }
    
    
    • 代理对象
    /**
     * 代理对象
     */
    public class Proxy implements Interface {
        private Interface proxied;
    
        public Proxy(Interface proxied) {
            this.proxied = proxied;
        }
    
        public void doSomething() {
            System.out.println("Proxy doSomething");
            proxied.doSomething();
        }
    
        public void somethingElse(String arg) {
            System.out.println("Proxy somethingElse" + arg);
            proxied.somethingElse(arg);
        }
    }
    
    • 测试
        /**
         * 测试代理,比较原对象与代理对象
         *
         * @param args
         */
        public static void main(String[] args) {
            Interface iface = new RealObject();
            iface.doSomething();
            iface.somethingElse("bonobo");
    
            Interface iface2 = new Proxy(iface);
            iface2.doSomething();
            iface2.somethingElse("bonobo");
        }
    

    动态代理

    Java动态代理可以动态创建代理并动态处理对所代理的方法的调用

    在动态里上所做的所有调用都会被重定向到单一的调用处理器上,它的工作是揭示调用的类型并确定对应的对策

    • 动态代理
    public class DynamicProxyHandler implements InvocationHandler {
        private Object proxied;
    
        public DynamicProxyHandler(Object proxied) {
            this.proxied = proxied;
        }
    
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("**** proxy:" + proxy.getClass() + ".method: " + method + ".args: " + args);
            if (args != null) {
                for (Object arg : args) {
                    System.out.println("    " + args);
                }
            }
            return method.invoke(proxied, args);
        }
    }
    
    • 测试
        public static void main(String[] args){
            RealObject real = new RealObject();
            real.doSomething();
            real.somethingElse("bonobo");
    
            Interface proxy = (Interface) Proxy.newProxyInstance(
                    Interface.class.getClassLoader(),
                    new Class[]{Interface.class},
                    new DynamicProxyHandler(real));
            proxy.doSomething();
            proxy.somethingElse("bonobo");
    
        }
    
  • 相关阅读:
    使用BeanShell 对比取出来的值
    https 请求的端口是443 注意
    Jmeter录制App 请求是HTTPS的
    Charles-断点
    随手记--分配事件概率
    看日志有没有 出现错误的字段 (如 crash ) 查找app闪退
    学习的网站
    xss 攻击 sql 注入
    app的apk 安装的方法--adb--命令安装 (含把apk放某个文件夹,每次启动自己安装)
    把2列相加的方法
  • 原文地址:https://www.cnblogs.com/bmbh/p/9154813.html
Copyright © 2011-2022 走看看