用JAVA实现Python的装饰器功能(AOP)

在 Java 中,我们可以使用注解来标记需要被装饰的方法或类,然后通过反射获取这些注解,并根据注解的信息对方法或类进行装饰。

我们假设现在需要实现一个装饰器,用来记录方法的执行时间。我们可以先定义一个注解 @LogExecutionTime,并将它应用在需要被装饰的方法上:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogExecutionTime {
}

这个注解没有任何属性,只是用来标记需要被装饰的方法。

接下来,我们定义一个装饰器类 LogExecutionTimeDecorator,用来对被 @LogExecutionTime 注解标记的方法进行装饰。这个装饰器类需要实现 InvocationHandler 接口,并重写它的 invoke 方法:

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

public class LogExecutionTimeDecorator implements InvocationHandler {
    private Object target;

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

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.isAnnotationPresent(LogExecutionTime.class)) {
            long startTime = System.currentTimeMillis();
            Object result = method.invoke(target, args);
            long endTime = System.currentTimeMillis();
            System.out.println("Method " + method.getName() + " took " + (endTime - startTime) + " ms");
            return result;
        } else {
            return method.invoke(target, args);
        }
    }
}

在这个装饰器类中,我们首先定义了一个 target 变量,用来保存需要被装饰的对象。在装饰器的构造方法中,我们将需要被装饰的对象传递进来,并保存到 target 变量中。

在 invoke 方法中,我们首先判断被调用的方法是否被 @LogExecutionTime 注解标记。如果被标记了,我们记录下方法开始执行的时间,并调用被装饰对象的方法。在方法执行完成后,我们记录下方法执行结束的时间,并打印出方法的执行时间。最后,我们返回方法的执行结果。

如果方法没有被 @LogExecutionTime 注解标记,我们直接调用被装饰对象的方法,并返回方法的执行结果。

最后,我们可以在需要被装饰的对象上使用 Proxy.newProxyInstance 方法来创建一个代理对象,并使用这个代理对象来调用被装饰的方法。以下是一个示例代码:

public class Main {
    public static void main(String[] args) {
        // 创建需要被装饰的对象
        MyService service = new MyServiceImpl();

        // 创建装饰器
        LogExecutionTimeDecorator decorator = new LogExecutionTimeDecorator(service);

        // 创建代理对象
        MyService proxy = (MyService) Proxy.newProxyInstance(
                Main.class.getClassLoader(),
                new Class<?>[]{MyService.class},
                decorator);

        // 调用被装饰的方法
        proxy.doSomething();
    }
}

改造一些main方法,新增一个代理实例,这里我们可以接受一个接口

class Factory {
    public static MyService factory(MyService object) {
        // 创建装饰器
        LogExecutionTimeDecorator decorator = new LogExecutionTimeDecorator(object);

        // 创建代理对象
        MyService proxy = (MyService) Proxy.newProxyInstance(
                Main.class.getClassLoader(),
                new Class<?>[]{MyService.class},
                decorator);
        return proxy;

    }
}
public class Main {
    public static void main(String[] args) {
        // 创建需要被装饰的对象
        MyService service = new MyServiceImpl();
        long l = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {

        System.out.println(Factory.factory(service).doSomething());
//            System.out.println(service.doSomething());
        }
        long ls = System.currentTimeMillis();
        System.out.println(ls - l);
    }
}

测试了一下用与不用的耗时差不多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值