设计模式之代理模式

一般有静态代理,动态代理,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包版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值