动态代理_基本实现步骤和增强方法

动态代理-基本实现步骤   

动态代理,增强对象Lenove
        
            三个参数:
                    1.类加载器:真实对象.getClass().getClassLoader()
                    2.接口数组:真实对象.getClass().getInterfaces()
                    3.处理器:new InvocationHandler()
代理逻辑编写的方法:代理对象调用的所有方法都会触发该方执行
             参数:
                 1.proxy:代理对象
                 2.method:代理对象调用的方法被封装为的对象
                 3.arg:代理对象调方法时,传递的实际参数
         

创建SaleComputer接口

package com.example.day_19_fitterlistener.proxy;

public interface SaleComputer {
    public String sale(double money);
    public void show();
}
复制代码
package com.example.day_19_fitterlistener.proxy;

public class Lenovo implements SaleComputer{
    @Override
    public String sale(double money) {
        System.out.println("花了"+money+"元,买了一根冰棍");
        return "冰棍";
    }

    @Override
    public void show() {
        System.out.println("展示冰棍");
    }
}
复制代码
复制代码
package com.example.day_19_fitterlistener.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
        //创建真实对象
        Lenovo lenovo = new Lenovo();

        //动态代理,增强对象Lenove
        /*
            三个参数:
                    1.类加载器:真实对象.getClass().getClassLoader()
                    2.接口数组:真实对象.getClass().getInterfaces()
                    3.处理器:new InvocationHandler()
         */
        SaleComputer proxy_lenovo = (SaleComputer) Proxy.newProxyInstance(lenovo.getClass().getClassLoader(), lenovo.getClass().getInterfaces(), new InvocationHandler() {

            /*
                代理逻辑编写的方法:代理对象调用的所有方法都会触发该方执行
                    参数:
                        1.proxy:代理对象
                        2.method:代理对象调用的方法被封装为的对象
                        3.arg:代理对象调方法时,传递的实际参数
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("该方法执行了");
                System.out.println(method.getName());
                System.out.println(args[0]);
                return null;
            }
        });

        //调用方法
        String sale = proxy_lenovo.sale(8000);
        System.out.println(sale);
          //  proxy_lenovo.show();

    }
}
复制代码

动态代理-增强方法

增强对象的功能:

设计模式:一些通用的解决固定问题的方式

    1. 装饰模式

    2. 代理模式

概念:

    1. 真实对象:被代理的对象

    2. 代理对象:

3. 代理模式:代理对象代理真实对象,达到增强真实对象功能的目的

实现方式:

    1. 静态代理:有一个类文件描述代理模式

    2. 动态代理:在内存中形成代理类

实现步骤:

    1. 代理对象和真实对象实现相同的接口

    2. 代理对象 = Proxy.newProxyInstance();

    3. 使用代理对象调用方法。

4. 增强方法

增强方式:

    1. 增强参数列表

    2. 增强返回值类型

    3. 增强方法体执行逻辑

复制代码
package com.example.day_19_fitterlistener.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
        //创建真实对象
        Lenovo lenovo = new Lenovo();

        //动态代理,增强对象Lenove
        /*
            三个参数:
                    1.类加载器:真实对象.getClass().getClassLoader()
                    2.接口数组:真实对象.getClass().getInterfaces()
                    3.处理器:new InvocationHandler()
         */
        SaleComputer proxy_lenovo = (SaleComputer) Proxy.newProxyInstance(lenovo.getClass().getClassLoader(), lenovo.getClass().getInterfaces(), new InvocationHandler() {

            /*
                代理逻辑编写的方法:代理对象调用的所有方法都会触发该方执行
                    参数:
                        1.proxy:代理对象
                        2.method:代理对象调用的方法被封装为的对象
                        3.arg:代理对象调方法时,传递的实际参数
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
               /*
                    System.out.println("该方法执行了");
                    System.out.println(method.getName());
                    System.out.println(args[0]);
                */

                //判断是否是sale方法
                if(method.getName().equals("sale")){
                    //增强参数
                    double money = (double) args[0];
                    money = money * 0.85;
                    System.out.println("专车接..");
                    //使用真实对象调用该方法
                    String  obj = (String) method.invoke(lenovo,money );
                    System.out.println("免费送货");
                    //增强返回值
                    return obj+"-鼠标垫";
                }else {
                    Object obj = method.invoke(lenovo,args);
                    return obj;
                }
            }
        });

        //调用方法
//        String sale = proxy_lenovo.sale(8000);
//        System.out.println(sale);
            proxy_lenovo.show();
    }
}
复制代码
posted @   夫君  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示