美文网首页
代理模式

代理模式

作者: Neo_xu | 来源:发表于2018-09-18 17:23 被阅读0次

    概念

    中介,就是为一个实际业务对象提供控制处理的代理。

    角色

    • 抽象对象
    • 业务处理对象
    • 代理对象

    分类

    • 静态代理
    • 动态代理

    demo

    // 代理类和业务类需要共同实现的接口
    public interface Person {
        public void tollage();
    }
    
    //被代理的业务类
    public class Employee implements Person {
        @Override
        public void tollage() {
            System.out.println("员工上缴个税");
        }
    }
    

    静态代理的实现方式

    //代理对象
    public class EmployeeProxy implements Person {
        private Employee  employee;
        public EmployeeProxy(Employee employee) {
            super();
            this.employee = employee;
        }
        @Override
        public void tollage() {
            System.out.println("代理上缴个税begin");
            employee.tollage();
            System.out.println("代理上缴个税end");
        }
    }
    
    //测试类
    public class StaticProxyTest {
        public static void main(String[] args) {
            Employee employee = new Employee();
            EmployeeProxy employeeProxy = new EmployeeProxy(employee);
            employeeProxy.tollage();
        }
    }
    
    

    动态代理的实现方式

    JDK proxy:代理类必须实现接口
    // 动态代理类
    public class EmployeeInvocationHandler implements InvocationHandler {   
        private Object target;
        public EmployeeInvocationHandler(Object target) {
            super();
            this.target = target;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("代理执行" +method.getName() + "方法");
                Object result = method.invoke(target, args);
                return result;
        }
    }
    
    //测试类
    public class DynamicProxyTest {
        public static void main(String[] args) {
            Employee employee = new Employee();
            EmployeeInvocationHandler employeeInvocationHandler = new EmployeeInvocationHandler(employee);
            Person person = (Person) Proxy.newProxyInstance(Person.class.getClassLoader(), new Class[] {Person.class} , employeeInvocationHandler);
            person.tollage();;
        }
    }
    
    CGLIB: 代理类不能是final,代理的方法也不能是final(继承限制)
    public class CglibProxy implements MethodInterceptor {
        /* (非 Javadoc) 
        * <p>Title: intercept</p> 
        * <p>Description: </p> 
        * @param  代理对象
        * @param 拦截的方法
        * @param 方法的参数
        * @param JDK的java.lang.reflect.Method类的代理类
        * @return
        * @throws Throwable 
        * @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], net.sf.cglib.proxy.MethodProxy) 
        */
        @Override
        public Object intercept(Object obj, Method method, Object[] arg2, MethodProxy methodProxy) throws Throwable {
            return  methodProxy.invokeSuper(obj, arg2);
        }
    }
    
    //测试类
    public class CglibProxyTest {
        public static void main(String[] args) {
            CglibProxy cglibProxy = new CglibProxy();
            
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(Employee.class);
            enhancer.setCallback(cglibProxy);
            
            Person person = (Person)enhancer.create();
            person.tollage();
        }
    }
    

    相关文章

      网友评论

          本文标题:代理模式

          本文链接:https://www.haomeiwen.com/subject/bhzigftx.html