面向切面编程(Aspect-oriented programming--AOP)

FROM: https://en.wikipedia.org/wiki/Aspect-oriented_programming

In computingaspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns

 It does so by adding additional behavior to existing code (an advicewithout modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification, such as "log all function calls when the function's name begins with 'set'". 

 This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality. 

在计算机中,面向切面的编程(AOP)是一种编程范式,旨在通过允许将交叉关注点分离来增加模块性。

它实现的方法是通过在现有代码(建议)中添加额外行为而不修改代码本身,接着通过“切入点”规范分别指定哪些代码被修改,例如“当函数的名字以'set'开始时记录所有函数调用”。

这允许将不是业务逻辑核心的行为(如日志记录)添加到程序中,而不会混淆代码和核心功能。

AOP includes programming methods and tools that support the modularization of concerns at the level of the source code,

Aspect-oriented programming entails breaking down program logic into distinct parts (so-called concerns, cohesive areas of functionality). 

AOP包括编程方法和工具,这些东西支持关注点(concern)在源代码层的模块化 

面向方面的编程需要将程序逻辑分解成不同的部分(也就是所谓的关注点--功能性的内聚区域)

 

Join point models 汇合点模型

The advice-related component of an aspect-oriented language defines a join point model (JPM). A JPM defines three things:

  1. When the advice can run. These are called join points because they are points in a running program where additional behavior can be usefully joined. A join point needs to be addressable and understandable by an ordinary programmer to be useful. It should also be stable across inconsequential program changes in order for an aspect to be stable across such changes. Many AOP implementations support method executions and field references as join points.
  2. A way to specify (or quantify) join points, called pointcuts. Pointcuts determine whether a given join point matches. Most useful pointcut languages use a syntax like the base language (for example, AspectJ uses Java signatures) and allow reuse through naming and combination.
  3. A means of specifying code to run at a join point. AspectJ calls this advice, and can run it before, after, and around join points. Some implementations also support things like defining a method in an aspect on another class.

Join-point models can be compared based on the join points exposed, how join points are specified, the operations permitted at the join points, and the structural enhancements that can be expressed.

 

术语 Terminology

横切关注点 Cross-cutting concerns

In aspect-oriented software developmentcross-cutting concerns are aspects of a program that affect other concerns. These concerns often cannot be cleanly decomposed from the rest of the system in both the design and implementation, and can result in either scattering (code duplication), tangling (significant dependencies between systems), or both.

切面 Aspect

An aspect of a program is a feature linked to many other parts of the program, but which is not related to the program's primary function. An aspect crosscuts the program's core concerns, therefore violating its separation of concerns that tries to encapsulate unrelated functions. For example, logging code can crosscut many modules, yet the aspect of logging should be separate from the functional concerns of the module it cross-cuts. Isolating such aspects as logging and persistence from business logic is the aim of aspect-oriented software development (AOSD), of which the aspect-oriented programming (AOP) paradigm is the most widely employed.

Advice

This is the additional code that you want to apply to your existing model. In our example, this is the logging code that we want to apply whenever the thread enters or exits a method.

切点 Pointcut

This is the term given to the point of execution in the application at which cross-cutting concern needs to be applied. In our example, a pointcut is reached when the thread enters a method, and another pointcut is reached when the thread exits the method.

 

总结:面向切面编程是一种编程范式,他通过允许分离程序的交叉关注点,用来增强程序的模块性,他是对面向对象编程的一种补充。通过AOP我们可以减少代码的重复,使程序的业务逻辑清晰,减少系统间的依赖。面向切面编程需要把程序的业务逻辑拆分为不同的部分(distinct part),这些不同的部分也叫关注点。

程序的关注点是指:编写程序时应实现的功能(你对这个程序应实现的功能的考虑)。

交叉关注点是指:从字面上可以理解为”和其他关注点交叉的关注点”,即在一个程序中会影响其他关注点的次要关注点。交叉关注点横切程序的主要功能(主要关注点)。

切点:切点的功能是定义汇合点

汇合点:插入交叉关注点的地方(point)就是AOP编程中的汇合点。

切面:在AOP中切面横切程序的次要关注点,他关联着程序中的其他部分,他和程序的主要功能无关。

 

水平有限,望多指教!

posted @ 2017-12-03 14:52  爱水果  阅读(661)  评论(0编辑  收藏  举报