设计模式之代理模式

﹥>﹥吖頭↗ 提交于 2019-12-25 16:26:17

一般有静态代理,动态代理,cglib

1.静态代理

/**
 * @Author: king
 * @Date: 2019/12/24 16:08
 * @Version 1.0
 * 抽象主题
 */
public interface IGeometry {
    public double getArea();
}
/**
 * @Author: king
 * @Date: 2019/12/24 16:10
 * @Version 1.0
 * 具体主题
 */
public class Triangle implements IGeometry {
    double sideA, sideB, sideC, area;

    public Triangle(double sideA, double sideB, double sideC) {
        this.sideA = sideA;
        this.sideB = sideB;
        this.sideC = sideC;
    }

    public double getArea() {
        double p = (sideA + sideB + sideC) / 2.0;
        area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
        return area;
    }
}
/**
 * @Author: king
 * @Date: 2019/12/24 16:16
 * @Version 1.0
 * 代理
 * 检查所代理对象是否可用
 */
public class TriangleProxy implements IGeometry {
    double sideA, sideB, sideC;
    Triangle triangle;

    public void setSide(double sideA, double sideB, double sideC) {
        this.sideA = sideA;
        this.sideB = sideB;
        this.sideC = sideC;
    }

    public double getArea() {
        if (sideA + sideB > sideC || sideB + sideC > sideA || sideC + sideA > sideB) {
            triangle = new Triangle(sideA, sideB, sideC);
            double area = triangle.getArea();
            return area;
        } else {
            return -1;
        }
    }
}

2.动态代理

/**
 * @Author: king
 * @Date: 2019/12/24 17:04
 * @Version 1.0
 * 抽象
 */
public interface IGeometry {
    public double getArea();
}
/**
 * @Author: king
 * @Date: 2019/12/24 17:05
 * @Version 1.0
 * 具体实现
 */
public class Triangle implements IGeometry {
    double sideA, sideB, sideC, area;

    public Triangle(double sideA, double sideB, double sideC) {
        this.sideA = sideA;
        this.sideB = sideB;
        this.sideC = sideC;
    }

    public double getArea() {
        double p = (sideA + sideB + sideC) / 2.0;
        area = Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
        return area;
    }
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Author: king
 * @Date: 2019/12/24 17:09
 * @Version 1.0
 * 动态代理
 * 反射
 */
public class TriangleProxy implements InvocationHandler {
    private Object target;

    public Object getInstance(Object target) {
        this.target = target;
        return java.lang.reflect.Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(target, args);
        return result;
    }
}

3.cglib

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: king
 * @Date: 2019/12/24 19:33
 * @Version 1.0
 * 代理
 */
public class TriangleProxy implements MethodInterceptor {
    private Object target;

    public TriangleProxy(Object target) {
        this.target = target;
    }

    public Object getInstance() {
        //1.工具
        Enhancer enhancer = new Enhancer();
        //2.设置父类
        enhancer.setSuperclass(target.getClass());
        //3.设置回调
        enhancer.setCallback(this);
        //4.创建子类(代理对象)
        return enhancer.create();
    }


    public Object intercept(Object obj, Method mentod, Object[] args, MethodProxy proxy) throws Throwable {
        //Object result = mentod.invoke(obj, args);
        proxy.invokeSuper(obj, args);
        return null;
    }
}

导入cglib.jar和asm.jar,注意jar包版本

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!