zoukankan      html  css  js  c++  java
  • java dyn proxy

    package dyn;
    
    public interface RealService {
        
        void buy();
    
    }
    
    ===================
    package dyn;
    
    public class RealServiceImpl implements RealService {
    
        @Override
        public void buy() {
            //do real service ,such as crud
            System.out.println("buy big hourse... ....");
        }
    
    }
    ===================
    
    package dyn;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    
    public class MyInvocationHandler implements InvocationHandler {
        //realObject
        private RealService target;
    
    
        public MyInvocationHandler(RealService target) {
            super();
            this.target = target;
        }
    
        @Override
        public Object invoke(Object arg0, Method method, Object[] obj)
                throws Throwable {
            System.out.println("make big money... ...");
            Object o = method.invoke(target,obj);
            System.out.println("kiss QQ");
            return o;
            
        }
    
        public Object getRealServiceProxyObj() {
            return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this);
        }
    
    }
    ===============================
    
    package dyn;
    
    public class TestDynProxy {
    
        public static void main(String[] args) {
            // reql obj
            RealService real = new RealServiceImpl();
            // trans  中间商
            MyInvocationHandler invocationHandler = new MyInvocationHandler(real);
            // proxy obj
            RealService realServiceProxyObj = (RealService)invocationHandler.getRealServiceProxyObj();
            
            realServiceProxyObj.buy();
    
        }
    }
  • 相关阅读:
    Nebula通关指南
    hackersonlineclub.com
    PHP设计的超强大的文件上传类(单文件上传)
    PHP面向对象实例(图形计算器)
    DIV+CSS布局网站基本框架
    python的threading模块
    Python 命令行参数和getopt模块详解
    python optparse模块
    python的sys模块
    python实现简单的netcat
  • 原文地址:https://www.cnblogs.com/alamps/p/5173258.html
Copyright © 2011-2022 走看看