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");
    
        }
    
  • 相关阅读:
    08.Linux系统-Fastdfs分布式文件系统-互为主从配置搭建部署
    07.Linux系统-GitLab版本控制服务安装部署
    06.Linux系统-WCP知识共享平台安装部署(旗舰版)
    01.Linux-CentOS系统清理缓存脚本
    15.Linux-CentOS系统重启网卡ping不通问题(云环境)
    14.Linux-CentOS系统proc文件系统丢失
    设置环境变量遇到的难题,cmd管理员方式与普通方式的区别,通过C#代码设置环境变量
    DataGridView 行数据验证:当输入数据无效时不出现红色感叹号的Bug
    VS2017新建项目的模板之配置
    禅道安装
  • 原文地址:https://www.cnblogs.com/bmbh/p/9154813.html
Copyright © 2011-2022 走看看