360 likes | 650 Views
Aspect Oriented Programming . Gülşah KARADUMAN. Outline. Introduction The Problem Goal of AOP Programming with Aspects AspectJ Conclusions. Background Information. Functional decomposition Break the system into smaller units Programming languages
E N D
Aspect Oriented Programming Gülşah KARADUMAN
Outline • Introduction • The Problem • Goal of AOP • Programming with Aspects • AspectJ • Conclusions
Background Information • Functional decomposition • Break the system into smaller units • Programming languages • Define abstractions for the small units • Compose the units into systems
Procedure Based Languages • Functional, Procedural, OO • Comfortable to talk about what is encapsulated as a functional unit of the overall system • What about the units of decomposition that are not functional
Modularity • Abstraction • Decomposition • Encapsulation • Information Hiding • Separation of Concerns • Low coupling • High cohesion
Concerns • Particular goal, concept, or area of interest • Primary motivation for organizing and decomposing software into manageable and comprehensible parts
Separation of Concerns • Cohesion • Maximizecohesion within a component • Coupling • Minimize coupling between components
Separation of Concerns • Advantages • Understandability • Reusability • Extensibility • Maintainability • Adaptability
The Problem • Limitations of OOP • decomposition resulting in cross-cutting concerns • Insufficieny of OO and procedural development techniques
Crosscutting, Scattering, and Tangling • Crosscutting • Concernsinherently relating to multiple components • Scattering • Single concern affecting multiple modules • Tangling • Multiple concerns interleaved in a single module
Crosscutting Concern Examples • Synchronization • Real-time constraints • Error-checking • Object interaction constraints • Memory management • Persistency • Security • Caching • Logging • Monitoring • Testing • Domain specific optimization
DisplayTracking Crosscutting Concern Examples Display * Figure FigureElement Point Line 2 getX() getY() getP1 setP1 setX(int) setY(int) setP1(Point) setP2(Point)
Cost of Crosscutting Concerns • Reduced understandability • Decreased adaptability • Decreased reusability • Decreased maintainability
Component • Cleanly encapsulated in a generalized procedure (i.e. object, method, procedure, API) • Well-localized, and easily accessed and composed
Aspect • Properties that affect the performance or semantics of the components in systemic ways • Not cleanly encapsulated in a generalized procedure
Goal of AOP • Separate components and aspects from each other • Abstract and compose the components and aspects to produce the overall system
AOP • A promising new technology for separating crosscutting concerns that are usually hard to do in object-oriented programming
Fundamentals of AOP • Aspect as a modularization unit • Three distinct development steps: • Aspectual decomposition • Concern implementation • Aspectual recomposition
Programming with Aspects • Writing the components • Writing the aspects • Weaving
Tools for AOP • AspectJ • AspectC++ • AspectWerkz • JAC • JBoss-AOP • Nanning
AspectJ • Small, well-integrated extension to Java • Java programs as input • .class files compatible with any JVM as output • Free and open source AspectJ compiler
AspectJ Terminology • Join point • Pointcut • Advice • Introduction • Aspect
Join Points • Well-defined points in a program's execution • Key points in dynamic call graph
Pointcuts • A named collection of join points • Designate join points
Advice • Before advice • After advice • Around advice
Introduction • Modifies • Members of a class • Relationship between classes
Aspect • Module for handling crosscutting concerns • Defined in terms of pointcuts, advice, and introduction • Reusable and inheritable
Example – Without AOP class Line { private Point _p1, _p2; Point getP1() { return _p1; } Point getP2() { return _p2; } void setP1(Point p1) { Tracer.traceEntry(“entry setP1”); _p1 = p1; Tracer.traceExit(“exit setP1”); } void setP2(Point p2) { Tracer.traceEntry(“entry setP2”); _p2 = p2; Tracer.traceExit(“exit setP2”); } class Point { privateint _x = 0, _y = 0; int getX() { return _x; } int getY() { return _y; } void setX(int x) { Tracer.traceEntry(“entry setX”); _x = x; Tracer.traceExit(“exit setX”) } void setY(int y) { Tracer.traceEntry(“exit setY”); _y = y; Tracer.traceExit(“exit setY”); } } class Tracer { staticvoid traceEntry(String str) { System.out.println(str); } staticvoid traceExit(String str) { System.out.println(str); } } Tangling Code Scattered Concern
pointcut advice Example - AspectJ aspect Tracing { pointcut traced(): call(* Line.* || call(* Point.*); before(): traced() { println(“Entering:” + thisjopinpoint); after(): traced() { println(“Exit:” + thisjopinpoint); void println(String str) {<write to appropriate stream>} } } aspect class Line { private Point _p1, _p2; Point getP1() { return _p1; } Point getP2() { return _p2; } void setP1(Point p1) { _p1 = p1; } void setP2(Point p2) { _p2 = p2; } } class Point { privateint _x = 0, _y = 0; int getX() { return _x; } int getY() { return _y; } void setX(int x) { _x = x; } void setY(int y) { _y = y; } } Aspect is defined in a separate module Crosscutting is localized No scattering; No tangling Improved modularity
Advantages of AOP • Tractability • Simpler cleaner code • Reusability • Easier to maintain
Disadvantages of AOP • Difficult to understand a software because of invisibly injected aspects • Fragile build problems • Complicated control flow breakage • Reduced quality ofsoftware if aspects are not appropriately managed
Conclusions • Scattered crosscutting concerns over several modules causing tangling code • Explicit abstraction mechanism with AOP • Increased modularity of the system
References • http://fsl.cs.uiuc.edu/images/9/9c/Kiczales97aspectoriented.pdf • http://en.wikipedia.org/wiki/Aspect-oriented_programming • http://www.developer.com/design/article.php/3308941
Thank you for your attention! • Questions?