现在的位置: 首页 > 综合 > 正文

Aspect-oriented programming

2013年10月09日 ⁄ 综合 ⁄ 共 2648字 ⁄ 字号 评论关闭

我们对面向过程和面向对象的编程语言已经耳熟能详了。

今天来看看Aspect-oriented programming,也就是面向行为的编程。

参考wiki,写的很不错:

http://en.wikipedia.org/wiki/Aspect-oriented_programming

  • 什么是AOP

In computing, aspect-oriented programming (AOP) is a programming paradigm which aims to increasemodularityby allowing the separation of cross-cutting concerns.这句话很重要!

  • 不太明白cross-cutting的同学可以去看看 aspect-oriented programming在spring中是如何使用的,一看就明白:

http://blog.csdn.net/onlyqi/article/details/6641332

  • Concerns的意思:Aspect-oriented programming entails breaking down program logic into distinct parts (so-calledconcerns, cohesive areas of functionality).
  • AOP includes programming methods and tools that support the modularization of concerns at the level of the source code, while "aspect-oriented software development" refers to a whole engineering discipline.

说的简单直白一些,和面向过程,面向对象的编程语言一样,AOP是一种编程思想。它可以通过编程语言的方法或工具来实现(例如在spring中使用AOP)。

AOP的核心思想就是使用cross-cutting concerns。简单的说,就是逻辑上相对独立的一段代码或单元,在整个程序代码中穿插重用。所以AOP的目的是让代码更灵活。

举两个例子便于大家理解:

1,上面建议大家去看的spring中AOP的应用,通过使用AOP,可以在程序中完全不同类型的两个对象运行各自的run()方法时,前后各打印一句话。这个应用充分体现了cross-cutting这个字眼所表达的意思。

2,

AOP最典型的利于理解的应用之一就在于记日志。用java为例,我们在记日志时,在多个类中总是希望使用同一个logger实例 (设计模式中为此还有一个模式叫singleton)。

这样就需要将一个logger实例在多个类中传递。但是使用AOP,我们就不需要传递logger,而是使用cross-cutting,所有的类就都可以使用logger了。

在使用AOP时,需要定义一系列的pointcut,或称为join points。例如:

 execution(* log*(*)) 代表所有类中,以log开头的方法,输入参数为一个任意类型的对象。

运行代码时,只要是能匹配上这个pointcut的方法,都将会被修改自身的行为。例如自动在运行log*()前运行一个setLogger(Logger logger)方法。

而这个setLogger(Logger logger)方法是在同一个地方被定义的(比如spring的配置文件),传递进去的Logger也是同一个对象。这样所有的类就都使用同一个Logger实例记录日志了。

正如大家所知,几乎所有的编程语言,都可以在一定程度上分组或封装代码,以便在别的地方重用。但AOP的特别之处在于:

But some concerns defy these forms of implementation and are called crosscutting concerns because they "cut across" multiple abstractions in a program.例如上面说的spring中的应用,一般的语言就难于做到。

  • AOP是如何实现的

对虚拟机有概念的同学一听就知道,这种AOP修改源代码的行为,必定要1,修改字节码 2,结合虚拟机才能实现。

An aspect can also make binary-compatible structural changes to other classes, like adding members or parents.

  • AOP在java中的应用: AspectJ

AspectJ不是独立的一个编程语言,而是在java语言中定义的aspect-oriented扩展。

为什么aspect-oriented没有像面向过程和面向对象那样,形成自己专门的编程语言呢?

非常简单,从AOP的核心思想就可以想象到,如果一门语言的核心就是AOP,那形成的代码该是多么的分散,来回穿插和相互纠结。该是多么的难懂和难于维护。

这就和大家都知道的goto一样,灵活过了头,就让人头疼了。

再回到spring上,我认为可以说,这是最好的AOP应用方式了。因为在Spring这个框架下使用AOP,可以让cross-cutting都在一个地方统一定义,而不是分散到代码中。

spring是java写的,其实就是spring这个框架中使用了AspectJ,然后又封装了一下,变成xml,可配置的形式而已。

  • AspectJ中的几个概念

AspectJ has a number of such expressions and encapsulates them in a special class, anaspect. For example, an aspect can alter the behavior ofthe base code (the non-aspect part of a program) by applying
advice (additional behavior) at variousjoin points (points in a program) specified in a quantification or query called a pointcut (that detects whether a given join point matches).

上例中,base code就是我们写好的run方法。advice就是我们定义的在run方法前后要打印的语句。join points就是我们定义的要插入advice的点,也就是run方法前后。

我准备在另外一篇文章中更具体的说AspectJ。

抱歉!评论已关闭.