AOP 是 Aspect Oriented Programming 的缩写,意思是面对方面编程,一种新兴的编程技术
AOP 实际是 GoF 设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦, AOP 可以说也是这种目标的一种实现
它可以解决 OOP 和过程化方法不能够很好解决的横切 (crosscut)问题, 如:事务、安全、日志等横切关注
当未来系统变得越来越复杂, 横切关注点就成为一个大问题的时候,AOP 就可以很轻松的解决横切关注点这个问题
比如有这样一个情景: Java 代码 1
public class AccountManager { 2
private static final sysLogger = SystemLogger
getInstance(); 3
private AuthorizationManager authMgr = new AuthorizationManager(); 4
public void transferFunds(String from, String to, int amount) { 6
sysLogger
log("transfer funds from " + from + " to " + to); 7
if(authMgr
accessAble(from) && authMgr
accessAble(to)) { 8
sysLogger
log("access successfully"); 9
CustomerAccount from = findAccount(from); 10
CustomerAccount to = findAccount(to); 11
debit(amount); 12
credit(amount); 13