面向方面程序設(shè)計
計算機專業(yè)時文選讀(971)
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.
面向方面程序設(shè)計
面向方面程序設(shè)計(AOP)是一種編程方法,允許以程序的整體特性來確定它如何編譯成可執(zhí)行程序。要說明AOP的概念,可以將方面編程類比成織布,這時紗線自動織成布。而沒有AOP,程序員必須用手來織布。
?
AOP是對面向?qū)ο蟪绦蛟O(shè)計的補充,它允許開發(fā)人員動態(tài)修改靜態(tài)的面向?qū)ο竽P?,以生成能成長的系統(tǒng),來滿足新的要求,從而讓應(yīng)用程序在發(fā)展過程中適應(yīng)新的特性。
?
AOP為把橫切代碼抽象提供了解決方案,而橫切代碼在沒有與這些代碼功能關(guān)聯(lián)性的情況下跨躍了對象的層次。AOP讓你將橫切代碼抽象進入另外的模塊(稱作方面),來替代將橫切代碼嵌入類,然后動態(tài)地在需要它的地方應(yīng)用這些代碼。通過定義對象模型中應(yīng)該應(yīng)用橫切代碼的具體地方(稱作點切),就能獲得橫切代碼的動態(tài)應(yīng)用。在運行時或編譯時,依據(jù)你的AOP架構(gòu),橫切代碼注入到特定的點切。從本質(zhì)上講,AOP允許你將新的功能引入對象,而對象不需要擁有任何有關(guān)引入的知識。
?
面向方面軟件開發(fā)(AOSD)是軟件開發(fā)的新方法,它要解決其他方法(如面向?qū)ο蟪绦蛟O(shè)計)中固有的局限性。AOSD通過提供系統(tǒng)的識別、分離、表示和組合等方法,目的是要解決橫切關(guān)注。橫切關(guān)注被封裝在其他的模塊中,稱作方面,從而可以促進本地化。這就帶來了對模塊化的更好支持,因此降低了開發(fā)、維護和升級的成本。
下面是在面向方面程序設(shè)計時常用的一些術(shù)語:
方面(Aspect)
方面是與一個程序的具體特性有關(guān)聯(lián)的子程序。當特性改變時,就會影響到整個程序。方面子程序用作一類稱作方面編織器的新編譯器的一部分。
方面是一個與類相似的結(jié)構(gòu),用于解決橫切類的關(guān)注。方面可以包含方法和屬性、擴展其他的類或方面以及實現(xiàn)接口。
AOP的方面將告知和點切引入功能單元,與面向?qū)ο蟪绦蛟O(shè)計使用類來包裝屬性和方法很相似。
AspectC++
對C++編程語言的面向方面的擴展。
AspectJ
對Java編程語言的面向方面的擴展。
忠告(Advice)
在滿足某些條件后運行的程序代碼。忠告允許你透明地將諸如登錄和規(guī)格等應(yīng)用于對象模型。
關(guān)注(Concern)
以一種代碼結(jié)構(gòu)實現(xiàn)的或者尚未實現(xiàn)的系統(tǒng)中所必需的某些功能或要求。關(guān)注的范圍上至高級的概念(如安全和服務(wù)質(zhì)量)、下至低級的概念(如高速緩存和普通緩存)。它們可以是功能性的,像特性或商業(yè)規(guī)則,或者是系統(tǒng)性的,如同步和交易管理。
橫切(Crosscutting)
如果與兩個關(guān)注有關(guān)的方法相交叉了,那么這兩個關(guān)注就橫切了。通過應(yīng)用相同的代碼,方面橫切了傳統(tǒng)的類和方法邊界。
截取器(Interceptor)
用于實現(xiàn)JBoss 4.0中的忠告,JBoss 4.0是一種流行的Java應(yīng)用服務(wù)器。
引入(Introduction)
將方法或?qū)傩约拥揭延蓄惿系姆绞剑蛘邔⒍鄠€繼承帶到普通的Java類上,或者將新的API(應(yīng)用編程接口)連到已有的對象模型。用于JBoss
4.0。
連接點(Joinpoints)
程序執(zhí)行中可以加入增強行為或附加上行為的點,即鉤子。例如,連接點能定義對類中特定方法的調(diào)用。
點切(Pointcuts)
指定連接點和收集這些點上具體的上下文的程序構(gòu)建。在AOP中,點切定義你的模型中的這些點,在這些點上應(yīng)用忠告。
編織(Weave)
按照提供的一些標準將不同的執(zhí)行邏輯段組裝編譯在一起,從而將一個關(guān)注裝入一個進程。 元數(shù)據(jù)(Metadata)
附加的信息,能在靜態(tài)或者運行時加到類上或者對象的給定事例中。元數(shù)據(jù)在編寫真正的通用方面時有幫助,這種方面能應(yīng)用于任何對象,但程序邏輯需要知道類特定的信息。
- 相關(guān)熱點:
- 行業(yè)英語學習
- 托福聽力詞匯