什么是 AspectJ 中类型间声明的简短示例,它演示了该方法的有用性? [英] What is a short example of inter-type declarations in AspectJ which demonstrates the usefulness of the approach?

查看:24
本文介绍了什么是 AspectJ 中类型间声明的简短示例,它演示了该方法的有用性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先考虑使用 ITD 来定义 private static final Logger logger = ... 用于一些不相关的情况,但它看起来不足以作为一个明显的改进来使用它作为演示示例.

I first thought about using ITDs to define the private static final Logger logger = ... for some unrelated cases, but it doesn't look enough like an obvious improvement to use that as a demonstration example.

是否有一些标准/建议的 ITD 使用示例,人们应该将其用于教学目的?

Is there some standard/suggested example of ITD usage, which people should use for teaching purposes?

推荐答案

我想在 Adam 的回答中添加另一个示例.

I'd like to add another example to Adam's answer.

使用方面是一种改进软件模块化的方法,OOP 也是,只不过 OOP 是垂直"应用的,而 AOP 是水平"应用的.

Using aspects is a way to improve modularization of your software, as well as OOP is, except that OOP is applied "vertically", while AOP is applied "horizontally".

因此,虽然通知可用于横向"修改响应某些标准的所有方法的行为,但通知中的代码也需要一些数据,并且 ITD 可以在横向"添加这些字段时同时包含在单个编译单元(aspect)中的相关数据和代码.

So, while advice can be used to modify "horizontally" the behavior of all methods responding to some criteria, aften code in advice will need also some data, and ITD makes it possible to add that fields "horizontally" while at the same time contain in a single compilation unit (the aspect) related data and code.

由于您是通过 ITD 字段添加数据,因此您可能还想提供一些方法来操作这些字段,例如 setter/getter.

Since you are adding data via ITD fields, you'll probably also want to offer some method to manipulate those fields, like a setter/getter.

我试着改写了几次,希望容易理解.

I tried to rephrase it a few times, I hope it's easy to understand.

举个例子,假设您有一个图形编辑程序,用户可以在其中创建、删除和显示许多形状(非常规范,嗯?:D).

To give you an example, suppose you have a graphical editing program, where the user can create, delete and display a number of Shapes (pretty canonical eh? :D ).

形状有很多数据,例如它们有颜色、坐标等,它们正确地属于类本身.但是,由于这是一个 web 3.0 应用程序,您希望用户能够在单个绘图上进行协作,这意味着每个形状必须知道何时更改,如果已向其他用户传达该形状已更改,是否可以被某些实际拖动它的用户锁定等.

Shapes have a lot of data, for example they have a color, coordinates etc.. which correctly belong to the class itself. However, since this is a web 3.0 application, you want users to be able to cooperate on a single drawing, which means each shape must know when it is changed, if it has been communicated to other users that the shape is changed, wether it is locked by some user that is actually dragging it etc..

您可以在简单的 OOP 中实现,在 Shape 或任何您的根类中实现相关部分,但是您将使用this.setDirty(true)"或类似的调用来污染所有的 setter.

You can do it in simple OOP, implementing relevant parts in Shape or whatever your root class is, but you will the pollute all your setters with calls like "this.setDirty(true)" or similar stuff.

相反,您决定采用 AOP 并在一个方面实现它.在这种情况下,您可以轻松地在所有 setter 之后添加一个建议,但您还需要存储一个脏"布尔值,或者如果您想优化仅发送更改增量的网络协议,甚至还需要存储更改的属性列表.

Instead, you decide to go AOP and implement it in an aspect. In this case, you can easily add an advice after all setters, but you'll also need to store a "dirty" boolean, or even a list of properties that changed if you want to optimize your network protocol sending only deltas of changes.

你可以像

public aspect ShapesAreDirtyThings {

   private boolean Shape.dirty;

   after(Shape s) : execution(* Shape+.set*(..)) {
       s.dirty = true;
       // Set some other global field, start a thread to communicate 
       // to other users about the change, whatever .. 
   } 

   public boolean Shape.isDirty() {
       return s.dirty;
   }

   public boolean Shape.findDirtyChildren() { // whatever
} 

这不是普通 AOP 无法做到的,它只是将建议、该建议所需的数据以及最终将对该数据进行操作的方法封装在单个编译单元中.

This is nothing you cannot do with common AOP, is simply a matter of encapsulating in a single compilation unit advice, data needed by that advice, and methods that will eventually operate on that data.

这篇关于什么是 AspectJ 中类型间声明的简短示例,它演示了该方法的有用性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆