Aspect-Oriented Programming
相關網站
文章
AspectJ
AOP builds on top of existing methodologies such as OOP and procedural programming, augmenting them with concepts and constructs in order to modularize crosscutting concerns.
在一個規模較大的軟體系統中,可以將其功能分為兩種,一種就是主要功能,另一種則是共同功能。
主要功能像是處理business logic。
共同功能則像是logging、authorization、persistence等等。
- AOP is not an antidote for bad or insufficient design.
- In fact, it is very tough to implement crosscutting concerns in a poorly designed core system.
- You will still need to create a solid core architecture using traditional design methodologies, such as OOP.
- What AOP offers is not a completely new design process, but an additional means that allow the architect to address future potential requirements without breaking the core system architecture, and to spend less time on crosscutting concerns during the initial design phase.
The AOP methodology
- Aspectual decomposition: Decompose the requirements to identify crosscutting and core concerns.
- Concern implementation: Implement each concern independently.
- Aspectual recomposition: Specify the recomposition rules by creating modularization units, or aspects. The actual process of recomposition, also known as weaving or integrating, uses this information to compose the final system.
Benefits of AOP
- Cleaner responsibilities of the individual module
- Higher modularization
- Easier system evolution
- Late binding of design decisions
- More code reuse
- Improved time-to-market
- Reduced costs of feature implementation
Myths and realities of AOP
True:
- The program flow in an AOP-based system is hard to follow
- AOP doesn't solve any new problems
- AOP breaks the encapsulation
False:
- AOP promotes sloppy design
- AOP is nice, but a nice abstract OOP interface is all you need
- The AOP compiler simply patches the core implementation
- AOP will replace OOP
Terms
- software system
- The realization of a set of concerns.
- concern (功能、考量)
- Specific requirement or consideration that must be addressed in order to satisfy the overall system goal.
- core concerns (主要功能、核心考量): Capture the central functionality of a module.
- crosscutting concerns (共同功能、交錯考量): Capture system-level, peripheral requirements that cross multiple modules. Such as
- authentication
- logging
- resource pooling
- administration
- performance
- storage management
- data persistence
- security
- multithread safety
- transaction integrity
- error checking
- policy enforcement
- aspect (面向)
- Unit of modularization that crosscuts other modules.
- aspect weaver (面向編織程序)
- Compiler-like entity, composes the final system by combining the core and crosscutting modules through a process called weaving.
History
多年前,學者發現要創造一個容易管理的系統,就必須要找出系統的功能、並將它們區隔開來,此稱為"separation of concerns" (SOC)。
到了1972年,David Parnas在論文中提出了一種達成SOC的方法,稱為模組化(modularization)。
OOP是個區隔主要功能(core concern)很好的方法,但卻沒有為共同功能(crosscutting concerns)提出適合的解決方式。
另外有許多方法被提出來要解決共同功能的問題,像是
- generative programming
- meta-programming
- reflective programming
- compositional filtering
- adaptive programming
- subject-oriented programming
- aspect-oriented programming
- intentional programming
其中最流行的就是aspect-oriented programming。
在AOP這個名稱正式被提出前的相關研究,是在全世界的一些大學中進行的。
其中又以在Palo Alto Research Center(PARC,Xerox的子公司)中的Cristina Lopes與Gregor Kiczales貢獻最大。
1996年,Gregor正式定出了"AOP"這個名稱,並帶領了一個在Xerox中的團隊創造了AspectJ。
AspectJ是AOP的一個實作(就像Java與SmallTalk是OOP的實作),後來Xerox把AspectJ轉為eclipse.org底下的一個open source專案。
目前OOP的實作有很多種,在Java的實作有AspectJ、Java Aspect Component (JAC)、Spring AOP、JBoss AOP...等;C的實作有AspectC;Python的實作有Pythius。
這些實作的不同處在於表現共同功能的方式,以及如何把這些功能整合到最後的系統中。
Reference