19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN
Jan 02, 2016
19.12.2006 1
Aspect Oriented Programming
Gülşah KARADUMAN
219.12.2006
Outline
Introduction The Problem Goal of AOP Programming with Aspects AspectJ Conclusions
319.12.2006
Background Information
Functional decomposition Break the system into smaller units
Programming languagesDefine abstractions for the small unitsCompose the units into systems
419.12.2006
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
519.12.2006
Modularity
Abstraction Decomposition Encapsulation Information Hiding Separation of Concerns
Low couplingHigh cohesion
619.12.2006
Concerns
Particular goal, concept, or area of interest Primary motivation for organizing and
decomposing software into manageable and comprehensible parts
719.12.2006
Separation of Concerns
CohesionMaximize cohesion within a component
CouplingMinimize coupling between components
819.12.2006
Separation of Concerns
AdvantagesUnderstandabilityReusabilityExtensibilityMaintainabilityAdaptability
919.12.2006
The Problem
Limitations of OOPdecomposition resulting in cross-cutting
concerns Insufficieny of OO and procedural
development techniques
1019.12.2006
Crosscutting, Scattering, and Tangling Crosscutting
Concerns inherently relating to multiple components
ScatteringSingle concern affecting multiple modules
TanglingMultiple concerns interleaved in a single
module
1119.12.2006
Crosscutting Concern Examples Synchronization Real-time constraints Error-checking Object interaction constraints Memory management Persistency Security Caching Logging Monitoring Testing Domain specific optimization
1219.12.2006
Crosscutting Concern Examples
Display
Figure FigureElement*
Point LinegetX()
getY()
getP1
setP1
DisplayTrackingsetX(int)
setY(int)
setP1(Point)
setP2(Point)
2
1319.12.2006
Cost of Crosscutting Concerns
Reduced understandability Decreased adaptability Decreased reusability
Decreased maintainability
1419.12.2006
Component
Cleanly encapsulated in a generalized procedure (i.e. object, method, procedure, API)
Well-localized, and easily accessed and composed
1519.12.2006
Aspect
Properties that affect the performance or semantics of the components in systemic ways
Not cleanly encapsulated in a generalized procedure
1619.12.2006
Goal of AOP
Separate components and aspects from each other
Abstract and compose the components and aspects to produce the overall system
1719.12.2006
AOP
A promising new technology for separating crosscutting concerns that are usually hard to do in object-oriented programming
1819.12.2006
Fundamentals of AOP
Aspect as a modularization unit Three distinct development steps:
Aspectual decomposition Concern implementation Aspectual recomposition
1919.12.2006
Steps of AOP
2019.12.2006
Programming with Aspects
Writing the components Writing the aspects Weaving
2119.12.2006
Aspect Weaving
2219.12.2006
Tools for AOP
AspectJ AspectC++ AspectWerkz JAC JBoss-AOP Nanning
2319.12.2006
AspectJ
Small, well-integrated extension to JavaJava programs as input.class files compatible with any JVM as
output Free and open source AspectJ compiler
2419.12.2006
AspectJ Terminology
Join point Pointcut Advice Introduction Aspect
2519.12.2006
Join Points
Well-defined points in a program's execution
Key points in dynamic call graph
2619.12.2006
Pointcuts
A named collection of join points Designate join points
2719.12.2006
Advice
Before advice After advice Around advice
2819.12.2006
Introduction
ModifiesMembers of a classRelationship between classes
2919.12.2006
Aspect
Module for handling crosscutting concernsDefined in terms of pointcuts, advice,
and introductionReusable and inheritable
3019.12.2006
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 {
private int _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”); }}
Example – Without AOP
Tangling Code
ScatteredConcern
class Tracer { static void traceEntry(String str) {
System.out.println(str); } static void traceExit(String str) {
System.out.println(str); }}
3119.12.2006
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 {
private int _x = 0, _y = 0;
int getX() { return _x; } int getY() { return _y; }
void setX(int x) { _x = x; } void setY(int y) { _y = y; }}
Example - AspectJaspect 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>}
}}
pointcut
advice
aspect
Aspect is defined in a separate moduleCrosscutting is localizedNo scattering; No tanglingImproved modularity
3219.12.2006
Advantages of AOP
TractabilitySimpler cleaner codeReusabilityEasier to maintain
3319.12.2006
Disadvantages of AOP
Difficult to understand a software because of invisibly injected aspects
Fragile build problems Complicated control flow breakage Reduced quality of software if aspects are not
appropriately managed
3419.12.2006
Conclusions
Scattered crosscutting concerns over several modules causing tangling code
Explicit abstraction mechanism with AOP Increased modularity of the system
3519.12.2006
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
3619.12.2006
Thank you for your attention!
Questions?