Java反射实例——动态代理

说动态代理之前,先来看一下静态代理的实现

//静态代理实例
public class StaticProxyTest {
    public static void main(String[] args) {
        ErkeClothFactory erkeClothFactory = new ErkeClothFactory();
        ClothFactory proxyClothFactory = new ProxyClothFactory(erkeClothFactory);
        proxyClothFactory.doCloth();
    }
}

//接口
interface ClothFactory{
    void doCloth();
}

//代理类
class ProxyClothFactory implements ClothFactory{
    private ClothFactory factory;

    public ProxyClothFactory(ClothFactory factory){
        this.factory = factory;
    }

    @Override
    public void doCloth() {
        System.out.println("代理工厂做一些准备操作");
        factory.doCloth();
        System.out.println("代理工程做一些后续操作");
    }
}

//被代理类
class ErkeClothFactory implements ClothFactory{

    @Override
    public void doCloth() {
        System.out.println("Erke正在生产衣服");
    }
}

动态代理

  • 区别于静态代理

    • 我们希望在编译过程中不写定代理类,在执行过程中通过反射技术来动态的生成代理类
  • 需要解决的问题

    • 如何动态的生成一个代理类的对象
      • 创建一个类,定义一个方法,调用Proxy.newProxyInstance方法来生成代理类
    • 在代理类中调用方法,被代理类怎么实现调用同名方法
      • 创建一个类实现InvocationHandler接口,重写invoke方法
    //动态代理举例
    public class ProxyTest {
        public static void main(String[] args) {
            //1.创建被代理类
            SuperMan superMan = new SuperMan();
            //2.创建代理类
            Human proxyClass = (Human) ProxyFactory.getProxyClass(superMan);
            //3.调用方法
            String belief = proxyClass.belief();
            System.out.println(belief);
            proxyClass.eat("炸鸡火腿汉堡包");
    
            System.out.println("************************");
            //为了展示动态性,我们调用一下静态代理的例子
            ErkeClothFactory erkeClothFactory = new ErkeClothFactory();
            ClothFactory clothFactory = (ClothFactory) ProxyFactory.getProxyClass(erkeClothFactory);
            clothFactory.doCloth();
        }
    }
    
    //定义一个接口
    interface Human{
        String belief();
        void eat(String food);
    }
    
    //被代理类
    class SuperMan implements Human{
    
        @Override
        public String belief() {
            return "I believe I can fly";
        }
    
        @Override
        public void eat(String food) {
            System.out.println("我喜欢吃" + food);
        }
    }
    
    
    class ProxyFactory{
        //调用此方法动态生成一个代理类,传入参数为被代理类
        public static Object getProxyClass(Object obj){
            MyInvocationHandler myInvocationHandler = new MyInvocationHandler(obj);
            return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),myInvocationHandler);
        }
    }
    
    class MyInvocationHandler implements InvocationHandler{
        private Object obj;
    
        public MyInvocationHandler(Object obj){
            this.obj = obj;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object invoke = method.invoke(obj, args);
            return invoke;
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值