Teddy's Knowledge Base

Why AOP?

Return to Contents

1. Why AOP?

Consider a typical e-commerce project. This kind a project may contain lots of parts besides the core implementing, such as logging, caching, security, and so on. When doing OOP, these parts are directly inserted into the core implementing. For example, a typical method implementing may start with some logging, security checking for parameters, then check for caching, and then the main processing after all processing, maybe there will be some logging again.

What problems will come to a project of this kind? First, logging, caching and security codes are in every method. Although we can extract these kinds of codes to separately libraries or at least separately classes and methods, but still there will be similar codes for calling them in every method. But these kinds of codes may not directly relate to the business logics, so there are bad smells. Second, in a typical development life cycle, we may focus on the core business logics first, and after the core implementing is ok, we do debugging and add logging, caching or security codes iteratively. Oh, of course, in this way, each time we must manually add these kinds of codes to every method. Each time! Every method! How crazy a thing!

Can we prevent these bad smells? Can we separate codes to independent modules, core implementing and other related logging, caching, security like modules, develop, debug them independently, and merge them when necessary easily with little extra effort? We Can! Let’s AOP!

posted on 2005-09-08 16:48 Teddy's Knowledge Base 阅读(3243) 评论(21)  编辑 收藏 网摘 所属分类: AOSD

评论

#1楼  2005-09-08 18:41 idior      

aop 很重要的一个特点就是实现了方法调用也只定义一次。
oo 只是提供了方法实现定义一次的方法, 而对于方法调用的散布没有办法。
  回复  引用  查看    

#2楼  2005-09-08 19:37 灵感之源      

以前一直没有去了解AOP是啥,有啥用。

今天用心看完你的随笔,终于了解到它的意义。

不知道怎样应用呢。
  回复  引用  查看    

#3楼  2005-09-08 20:31 34 [未注册用户]

毛病,这么蹩脚的东西也放在首页,丢人现眼。   回复  引用    

#4楼  2005-09-08 20:57 xiaoyuer      

logging, caching 之类的东西用AOP实现是不完美的,当玩具例子研究还行。如果你的 logging需要上下文相关,你怎么用AOp处理?   回复  引用  查看    

#5楼 [楼主] 2005-09-08 21:11 Teddy's Knowledge Base      

@xiaoyuer:

从本质上,基于AOP的logging并不绝对限制不能log上下文相关的内容,只是限制不能在Advice中定义存于本地Stack的变量。当然,的确AOP不是十分适合像错误日志这样的包含和特定上下文相关的定制信息,也因此,并不能说AOP就能完全代替传统的日志,也不是说AOP就能完全代替OOP。但是,AOP提供了一种分离Aspect的新的设计模式。对于日志而言,它非常适合一类trace方法调用的日志,当然不可能适合一切应用情景。实际上也没有哪一种语言,哪一种设计模式,甚至哪一种软件工程的方法学是适合一切的。用你的话说,当然不可能有完美的东西!所以我们要去了解它的优点和限制,去发挥它的长处,从而benefit我们的开发过程。
  回复  引用  查看    

#6楼  2005-09-08 21:29 idior      

@34
你先来点不蹩脚的再说!

---
如果你的 logging需要上下文相关,你怎么用AOp处理?
---
当然可以处理, 在aspect#中就已经可以获得被拦截方法的信息,甚至被拦截对象的信息!
  回复  引用  查看    

#7楼 [楼主] 2005-09-08 21:58 Teddy's Knowledge Base      

网上摘录的一段话:

Coming from the trenches of commercial software development, I don't expect AOP to be a magic bullet that eliminates the old problems of unanticipated change and the desire to build truly reusable and extensible components, but the difficulty of achieving that goal.

However, the industry is already embracing "disciplined" refactoring as the correct approach to changing requirements and AOP will become a powerful tool for this approach. Just as the best OO designs are based on abstractions (interfaces), so the best AO designs will be too.
  回复  引用  查看    

#8楼 [楼主] 2005-09-08 22:31 Teddy's Knowledge Base      

另一段老外专家关于基于AOP的logging的论述:

My advice would be this: handle the common and consistent cases - typically entry and exit plus exception/failure logging using aspects. Try and implement a strong oo design with small well-named methods each doing one
thing. It's amazing how many would-have-been ad-hoc debug statements actually fall on stable join points such as method executions with a solid underlying design and implementation. Once you've got that base in place,
another common category of join points that might help remove the need for ad-hoc debug statements are call join points to external components and libraries - these interactions are often of interest. Now take a good look
at what's left. If you need debug logging that doesn't fall on some stable join point and doesn't fit any consistent pattern, go ahead and code the debug logging call inline. If you want to retain flexibility to maybe remove
the debug logging altogether or to replace the implementation it's easy to create a simple abstraction over a single "debug" call. Personally I wouldn't be averse to writing Logging.aspectOf(this).debug("some stuff") in
my code either. But then I don't adhere to a strict base/aspect separation. I'm just writing a program with the best modularity I can and using types to do it. Some of those types are classes and some are aspects - I just use the best tool for the job in hand...

-- Adrian
Adrian_Colyer at uk.ibm.com

  回复  引用  查看    

#9楼  2005-09-09 09:10 Kevin      

的确,AOP不是万能药,其只能适用于很有限的场合。但就是在这样有限的场合中,给我们带来的帮助是很大的。   回复  引用  查看    

#10楼  2005-09-09 09:16 小陆      

logger,security等等只是为了解释aop概念使用的一些简单的例子,就像是解释oop的时候大家经常用shape和circle做例子一样。真实的应用场景是千差万别的,比这个复杂的多,也有用的多。
真实系统中很多事情都可以抽象成aspect,比如一个通信系统,最核心的功能是进行各种通信功能,在这个基础上还有其他方面的功能,如计费、客户管理、统计分析等等,这些方面就可以看作一个个的aspect。这些方面之间的交互就会变得简化,也会更加灵活,将能够更好的适应需求的变化。
aop象oop一样,可以用来解决很小的问题,比如一个按钮和一个窗体的交互过程,也可以用来解决很大的问题,比如一个庞大系统的设计思路。
  回复  引用  查看    

#11楼  2005-09-10 21:42 34 [未注册用户]

那个叫idior 的脑子进浆糊了,难道说别人球踢的臭,就需要自己先踢几把?   回复  引用    

#12楼 [楼主] 2005-09-10 22:12 Teddy's Knowledge Base      

@34:

大家来这里是交流技术的,有意见可以说,诚恳的批评别人也都会接受,就算你满腹经纶,不说出来点能体现你能力的东西,别怪别人不尊重你。你这种动不动的人身攻击,就只会被当成放屁,大家都懒得理你,你别不识抬举!
  回复  引用  查看    

#13楼  2005-09-14 19:05 蛙蛙池塘      

看不懂   回复  引用  查看    

#14楼  2005-09-15 17:04 clamphammer [未注册用户]

aop 很重要的一个特点就是实现了方法调用也只定义一次。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

idior的解释真不错!
  回复  引用    

#15楼  2005-10-17 12:01 qilong      

越来越知道自己的无知了   回复  引用  查看    

#16楼  2005-10-18 17:31 tom.wu [未注册用户]

Good!   回复  引用    

#17楼  2006-01-18 16:30 MoMo22 [未注册用户]

===随手翻译了下,水平有限,尚不能领会,都是直译=====
考虑某种业务相关的项目,这种项目可能包含很多基本的实现代码,例如 日志,缓存,安全机制等等。当我们用OOP(面向对象)的方法来做的时候,这些部分是直接放入到对象代码中去实现的。例如,一个方法调用前先要记录日志,再检查参数的安全性,然后检查缓存,才调用此方法,最后还要再写一次日志。
这样的项目会带来什么样的问题呢? 首先,日志,缓存,安全机制这些代码在几乎是存在于每一个方法中的,既然这些代码可以被封装成独立的Dll,或者是一个类,一个方法,那为什么我们不用类似的方法,把调用这些Dll,类,方法的调用代码也分离出来?因为这些调用代码都是与业务逻辑无关的,所以放在业务对象中感觉有点问题。第二,在系统开发生命周期内,我们都是先专注与业务逻辑,当业务逻辑代码实现以后,我们才会做调试,加入日志,缓存,安全控制等等代码,哇,这样的话,每次偶们写的方法都必须把这些代码加进去,每一次,每一个方法都要这样做,多crazy 的事啊!!
我们如何解决这种问题呢?我们可以把代码分解成独立的模块吗? 分成业务逻辑的实现模块,以及其他日志,缓存,安全控制等模块,独立的开发,调试吗,更重要的是我们可以在需要的时候,用一些简单的方法把这些模块整合起来吗?回答就是 AOP 。
  回复  引用    

#18楼  2006-01-23 08:48 iicup [未注册用户]

感谢 MoMo22 的翻译。   回复  引用    

#19楼  2006-02-27 14:57 蛙蛙池塘      

希望teddy本人发布英文文章的时候随手给弄个中文对照行不?   回复  引用  查看    

#20楼  2006-07-29 18:46 温家宝 [未注册用户]

我是温家宝要干劲杀绝这个网站.我的联系电话110.   回复  引用    

#21楼  2007-05-20 09:39 lb [未注册用户]

lz内容写的蛮好,不过英文实在不咋的   回复  引用    


发表评论



姓名 [登录] [注册] 
主页
Email (仅博主可见) 
验证码 *  验证码看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论   新用户注册   返回页首      

导航: 网站首页 社区 新闻 博问 闪存 网摘 招聘 .NET频道 知识库 找找看 Google站内搜索



China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务

相关文章:

相关链接: