• Java代理模式


    代理模式,顾名思义,就是想要访问真实对象时,需要先和中间人接触,让中间人去接触真实对象,最后把我想要的一起拿回来。

    interface Interface{
        void doSomething();
        void somethingElse(String arg);
    }
    class RealObject implements Interface{
        public void doSomething() {System.out.println("我是doSomething");}
        public void somethingElse(String arg) {System.out.println("somethingEles"+arg);}
    }
    class SimpleProxy implements Interface{
        private Interface proxied;
        public SimpleProxy(Interface proxied){this.proxied = proxied;}
        public void doSomething() {
            System.out.println("我是代理哦");
            proxied.doSomething();
        }
        public void somethingElse(String arg) {
            System.out.println("我是代理哦哦"+arg);
            proxied.somethingElse(arg);
        }
        public static void consumer(Interface iface){
            iface.doSomething();
            iface.somethingElse("boona");
        }
        public static void main(String[] args) {
            consumer(new RealObject());
            consumer(new SimpleProxy(new RealObject()));
        }
        /*  控制台打印信息:
            我是doSomething
            somethingElesboona
            我是代理哦
            我是doSomething
            我是代理哦哦boona
            somethingElesboona */
    }
    

    以上代码实现了静态代理,下面我们来分析一下代码。

    一个接口,一个实现接口的真实对象类,一个实现接口的代理对象类。

    静态代理的缺点:

    假如现在接口变成了另一个接口,那么真实对象类需要重写,代理对象类也需要重写。也就是说,代理对象对真实对象所作的所有代理操作全部需要重写。代码将无法复用。

    动态代理:

    而JDK实现的动态代理,就是把代理对象对真实对象所作的操作代码抽象出来,也就是说即使接口A变成了接口B,这个代理的所有操作还是可以用的,这是因为动态代理处理器将代理对象proxied设置成Object,所以可以通用。

    interface Interface{
        void doSomething();
        void somethingElse(String arg);
    }
    class RealObject implements Interface{
        public void doSomething() {System.out.println("我是doSomething");}
        public void somethingElse(String arg) {System.out.println("somethingEles"+arg);}
    }
    class DynamicProxyHandler implements InvocationHandler {
        Object proxied = null;
        public DynamicProxyHandler(Object t){this.proxied = t;}
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("目前的方法:"+method+"参数:"+args);
            //利用反射调用当前的方法
            return method.invoke(proxied, args);
    	}
    }
    class SimpleDynamicProxy{
        public static void consumer(Interface iface){
            iface.doSomething();
            iface.somethingElse("boona");
        }
        public static void main(String[] args) {
            RealObject object = new RealObject();
            consumer(object);
            //生成代理类
            Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
                        new Class[] { Interface.class }, new DynamicProxyHandler(object));
            consumer(proxy);
        }
        /*  控制台打印信息:
            我是doSomething
            somethingElesboona
            目前的方法:public abstract void study.Interface.doSomething()参数:null
            我是doSomething
            目前的方法:public abstract void study.Interface.somethingElse(java.lang.String)参数:[Ljava.lang.Object;@6bc7c054
            somethingElesboona
        */
    }
    

    这时候代码只有
    一个接口,一个实现接口的真实对象类。
    代理对象类被抽象成了公共接口。

  • 相关阅读:
    Sort
    RAID
    LeetCode总结 -- 一维动态规划篇
    Count and Say
    Dynamic Programming Introduction
    Mongodb与Redis应用指标对比
    精通有状态vs无状态(Stateful vs Stateless)—Immutable模式之姐妹篇
    Windows 安装 pytorch3d
    GitHub 图片无法显示 或 gist 无法访问
    LaTeX符号表,数学公式速查必备
  • 原文地址:https://www.cnblogs.com/bihanghang/p/9994558.html
Copyright © 2020-2023  润新知