一、代理模式:静态代理与动态代理

静态代理

  • 注:实质就是使代理类和被代理类实现同一个接口,然后在代理类中添加其他操作。

  • MyProxy.java文件

    package cn.qujialin.proxy;
    //代理类
    public class MyProxy implements Operator{
        private Operator operator;
    
        public MyProxy(Operator operator){
            this.operator = operator;
        }
    
        @Override
        public int add(int i, int j) {
            System.out.println("proxy前");
            operator.add(i,j);
            System.out.println("proxy后");
            return 0;
        }
    
        @Override
        public int sub(int i, int j) {
            System.out.println("proxy前");
            operator.sub(i,j);
            System.out.println("proxy后");
            return 0;
        }
    
        @Override
        public int mul(int i, int j) {
            System.out.println("proxy前");
            operator.mul(i,j);
            System.out.println("proxy后");
            return 0;
        }
    
        @Override
        public int div(int i, int j) {
            System.out.println("proxy前");
            operator.div(i,j);
            System.out.println("proxy后");
            return 0;
        }
    }
    //被代理类
    class MyOperator implements Operator{
        @Override
        public int add(int i, int j){
            System.out.println("add......");
            return i + j;
        }
        @Override
        public int sub(int i, int j){
            System.out.println("sub......");
            return i - j;
        }
        @Override
        public int mul(int i, int j){
            System.out.println("mul......");
            return i * j;
        }
        @Override
        public int div(int i, int j){
            System.out.println("div......");
            return i / j;
        }
    
    }
    
  • Operato.java

    package cn.qujialin.proxy;
    //接口
    public interface Operator {
        public int add(int i, int j);
        public int sub(int i, int j);
        public int mul(int i, int j);
        public int div(int i, int j);
    }
  • TestMain.java

    package cn.qujialin.proxy;
    //测试
    public class TestMain {
        public static void main(String[] args) {
            Operator op = new MyProxy(new MyOperator());
            op.add(1,2);
        }
    }

动态代理

  • MyActiveProxyFactory.java

    package cn.qujialin.activeproxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    //代理工厂
    public class MyActiveProxyFactory {
    
        public static Object getProxyInstance(Operator op){
            Object proxy = null;
            ClassLoader cl = op.getClass().getClassLoader();
            Class[] interfaces = op.getClass().getInterfaces();
            InvocationHandler invocationHandler = new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    String methodName = method.getName();
                    System.out.println("methodName:::" + methodName);
                    Object result = method.invoke(op, args);
                    System.out.println("methodName:::" + methodName);
                    return result;
                }
            };
            return Proxy.newProxyInstance(cl,interfaces,invocationHandler);
        }
    
    }
  • Operator.java

    package cn.qujialin.activeproxy;
    //接口
    public interface Operator {
        public int add(int i, int j);
        public int sub(int i, int j);
        public int mul(int i, int j);
        public int div(int i, int j);
    }
  • MyOperator.java

    package cn.qujialin.activeproxy;
    
    import cn.qujialin.activeproxy.Operator;
    
    //被代理类
    public class MyOperator implements Operator{
        @Override
        public int add(int i, int j){
            System.out.println("add......");
            return i + j;
        }
        @Override
        public int sub(int i, int j){
            System.out.println("sub......");
            return i - j;
        }
        @Override
        public int mul(int i, int j){
            System.out.println("mul......");
            return i * j;
        }
        @Override
        public int div(int i, int j){
            System.out.println("div......");
            return i / j;
        }
    
    }
    
  • TestMain.java

    package cn.qujialin.activeproxy;
    //测试
    public class TestMain {
        public static void main(String[] args) {
            Operator p = (Operator)MyActiveProxyFactory.getProxyInstance(new MyOperator());
            p.add(3,5);
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值