Thursday, July 13, 2006
Notes for 「Aspect-Oriented Programming: What is it good for?」
原文:Aspect-Oriented Programming: What is it good for?
A review of AOP concepts是個蠻簡潔的AOP介紹
描述AOP主要去解決的問題:
Aspects are concerns that are crosscutting. That is, they are not easily encapsulated in a single class. Yet, if we strictly follow the object-oriented paradigm, we need to represent such concerns in a uniform, maintainable manner. This often involves delegating the crosscutting responsibility to a separate helper class and relying on every class that requires the functionality expressed by the concern to include calls to the delegate at the appropriate places.另外作者用「A query of your code that returns a set of join points.」來比喻pointcut
感覺起來還蠻容易理解的,以後我會記住這個譬喻
接下來講了一個在The AspectJTM Programming Guide裡面的Execution tracing例子
這個例子其實如果你的Eclipse有灌AJDT的話
在File→New裡面就可以找到了
根據這個例子,作者所提出AOP的優點為:
- You have all of your source code pertaining to the tracing concern in one (two aspects) place.
- It is easy to insert and remove the tracing code. You simply remove the aspects from the build configuration.
- The trace code is everywhere you want it, even if you add new methods to the target classes. This eliminates human error. You also know that all trace code is removed, and you haven't overlooked anything when you remove the aspect from your build configuration.
- You have a reusable aspect that can be applied and enhanced.
作者提到了Design by Contract的概念:
This principle asserts that the designer of a class and the user of that class share assumptions about the class implementation. The contract includes invariants, preconditions, and post conditions. Design by Contract lets the class designer concentrate on the logic that implements the class functionality without worrying about the validity of arguments. This is, of course, if the contract states the preconditions for the arguments. Design by Contract avoids extra code and improves performance, as long as all clients of a class abide by the contract.
而defensive programming也可以用AOP來實踐
文中作者就舉了一個argument checking的例子
也提出了對於這類應用的結論:
In a large project where you have many classes and expose several interfaces, you might organize your code with a separate directory for the aspects that implement argument checking. I can imagine several ways of organizing the aspects so they are easily identifiable and maintainable. When you build the system for internal use, you use an internal build configuration, and when you build it for external use, you use a configuration that includes the aspects. The Eclipse AJDT makes the creation of new build configurations simple.
再來就是相對於Object Oriented Design Patterns (OODP)的Aspect Oriented Design Patterns (AODP)了
作者簡單說明了一個Adapter的例子
我想這主題其實大到可以出一本書
事實上許多AOP、AspectJ的書裡面或多或少都會有一部份這樣的內容
有興趣研究的還是上Aspect-Oriented Design Pattern Implementations這個網站
抓一些paper與source code看看吧
最後作者說AOP遲早會成為軟體開發者的一種工具
AspectJ 5、Eclipse+AJDT就是目前想要深入AOP領域的首選
AOP、AODP都還有很大的研究空間
還有作者最後一個Notes很有意思:
If any of the readers are using AOP and are willing to talk to me about their experiences, and possibly provide input for a research study I'm working on to develop AOP metrics, please contact me.
這句話我學起來了 :P