摘要:Aspect-OrientedProgrammingAspect-orientedprogramming(AOP)isanapproachtoprogrammingthatallowsglobalpropertiesofaprogramtodeterminehowitiscompiledintoanexecutableprogram.TheconceptualizersofAOPcompareaspectprogrammingtothemanufacturingofclothinwhichthreadsare
Aspect-Oriented Programming
Aspect-oriented programming (AOP) is an approach to programming that allows global properties of a program to determine how it is compiled into an executable program. The conceptualizers of AOP compare aspect programming to the manufacturing of cloth in which threads are automatically interwoven. Without AOP, programmers must stitch the threads by hand.
AOP complements object-oriented programming by allowing the developer to dynamically modify the static object-oriented model to create a system that can grow to meet new requirements, allowing an application to adopt new characteristics as it develops.
AOP provides a solution for abstracting cross-cutting code that spans object hierarchies without functional relevance to the code it spans. Instead of embedding cross-cutting code in classes, AOP allows you to abstract the cross-cutting code into a separate module (known as an aspect) and then apply the code dynamically where it is needed. You achieve dynamic application of the cross-cutting code by defining specific places (known as pointcuts) in your object model where cross-cutting code should be applied. At runtime or compile time, depending on your AOP framework, cross-cutting code is injected at the specified pointcuts. Essentially, AOP allows you to introduce new functionality into objects without the objects’ needing to have any knowledge of that introduction.
Aspect-oriented software development (AOSD) is a new approach to software development that addresses limitations inherent in other approaches, including object-oriented programming. AOSD aims to address crosscutting concerns by providing means for systematic identification, separation, representation and composition. Crosscutting concerns are encapsulated in separate modules, known as aspects, so that localization can be promoted. This results in better support for modularization hence reducing development, maintenance and evolution costs.
The following terms are often used in AOP:
Aspect An aspect is a subprogram that is associated with a specific property of a program. As that property varies, the effect “ripples” through the entire program. The aspect subprogram is used as part of a new kind of compiler called an aspect weaver.
Aspect is a construct, resembling classes, for addressing concerns that cut across classes. An aspect can contain methods and fields, extend other classes or aspects, and implement interfaces.
Aspects in AOP package advice and pointcuts into functional units in much the same way that object-oriented programming uses classes to package fields and methods
AspectC++ An aspect-oriented extension to the C++ programming language.
AspectJ An aspect-oriented extension to the Java programming language.
Advice Code that runs after certain conditions are met. Advices allow you to transparently apply things like logging and metrics to an object model..
Concern Some functionality or requirement necessary in a system that may or may not have been implemented in a code structure. Concerns can range from high-level notions like security and quality of service to low-level notions such as caching and buffering. They can be functional, like features or business rules, or systemic, such as synchronization and transaction management.
Crosscutting Two concerns crosscut if the methods related to those concerns intersect. An aspect crosscuts the traditional class and method boundaries by applying the same code in each.
Interceptor Used to implement an advice in JBoss 4.0, a popular Java application server.
Introduction A way to add methods or fields to an existing class, to bring multiple inheritance to plain Java classes or to attach a new API to an existing object model. Used in JBoss 4.0.
Joinpoints Points, or hooks, in a program’s execution where enhancements can be added or behaviors attached. For example, joinpoints could define calls to specific methods in a class.
Pointcuts Program constructs that designate joinpoints and collect specific context at those points. In AOP, pointcuts define the points in your model where advice will be applied.
Weave To assemble an individual concern into a process by interlacing different execution-logic fragments according to some supplied criteria.
Metadata Additional information that can be attached to a class or a given instance of an object, either statically or at runtime. Metadata helps when writing truly generic aspects that can be applied to any object, but the logic needs to know class-specific information.
面向方面程序設計
面向方面程序設計(AOP)是一種編程方法,允許以程序的整體特性來確定它如何編譯成可執行程序。要說明AOP的概念,可以將方面編程類比成織布,這時紗線自動織成布。而沒有AOP,程序員必須用手來織布。
AOP是對面向對象程序設計的補充,它允許開發人員動態修改靜態的面向對象模型,以生成能成長的系統,來滿足新的要求,從而讓應用程序在發展過程中適應新的特性。
AOP為把橫切代碼抽象提供了解決方案,而橫切代碼在沒有與這些代碼功能關聯性的情況下跨躍了對象的層次。AOP讓你將橫切代碼抽象進入另外的模塊(稱作方面),來替代將橫切代碼嵌入類,然后動態地在需要它的地方應用這些代碼。通過定義對象模型中應該應用橫切代碼的具體地方(稱作點切),就能獲得橫切代碼的動態應用。在運行時或編譯時,依據你的AOP架構,橫切代碼注入到特定的點切。從本質上講,AOP允許你將新的功能引入對象,而對象不需要擁有任何有關引入的知識。
面向方面軟件開發(AOSD)是軟件開發的新方法,它要解決其他方法(如面向對象程序設計)中固有的局限性。AOSD通過提供系統的識別、分離、表示和組合等方法,目的是要解決橫切關注。橫切關注被封裝在其他的模塊中,稱作方面,從而可以促進本地化。這就帶來了對模塊化的更好支持,因此降低了開發、維護和升級的成本。
下面是在面向方面程序設計時常用的一些術語:
方面(Aspect)
方面是與一個程序的具體特性有關聯的子程序。當特性改變時,就會影響到整個程序。方面子程序用作一類稱作方面編織器的新編譯器的一部分。
方面是一個與類相似的結構,用于解決橫切類的關注。方面可以包含方法和屬性、擴展其他的類或方面以及實現接口。
AOP的方面將告知和點切引入功能單元,與面向對象程序設計使用類來包裝屬性和方法很相似。
AspectC++
對C++編程語言的面向方面的擴展。
AspectJ
對Java編程語言的面向方面的擴展。
忠告(Advice)
在滿足某些條件后運行的程序代碼。忠告允許你透明地將諸如登錄和規格等應用于對象模型。
關注(Concern)
以一種代碼結構實現的或者尚未實現的系統中所必需的某些功能或要求。關注的范圍上至高級的概念(如安全和服務質量)、下至低級的概念(如高速緩存和普通緩存)。它們可以是功能性的,像特性或商業規則,或者是系統性的,如同步和交易管理。
橫切(Crosscutting)
如果與兩個關注有關的方法相交叉了,那么這兩個關注就橫切了。通過應用相同的代碼,方面橫切了傳統的類和方法邊界。
截取器(Interceptor)
用于實現JBoss 4.0中的忠告,JBoss 4.0是一種流行的Java應用服務器。
引入(Introduction)
將方法或屬性加到已有類上的方式,或者將多個繼承帶到普通的Java類上,或者將新的API(應用編程接口)連到已有的對象模型。用于JBoss 4.0。
連接點(Joinpoints)
程序執行中可以加入增強行為或附加上行為的點,即鉤子。例如,連接點能定義對類中特定方法的調用。
點切(Pointcuts)
指定連接點和收集這些點上具體的上下文的程序構建。在AOP中,點切定義你的模型中的這些點,在這些點上應用忠告。
編織(Weave)
按照提供的一些標準將不同的執行邏輯段組裝編譯在一起,從而將一個關注裝入一個進程。
元數據(Metadata)
附加的信息,能在靜態或者運行時加到類上或者對象的給定事例中。元數據在編寫真正的通用方面時有幫助,這種方面能應用于任何對象,但程序邏輯需要知道類特定的信息。
軟考備考資料免費領取
去領取