19.12.2006 1 aspect oriented programming gülşah karaduman

36
19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

Upload: vincent-cox

Post on 02-Jan-2016

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

19.12.2006 1

Aspect Oriented Programming

Gülşah KARADUMAN

Page 2: 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

Page 3: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

319.12.2006

Background Information

Functional decomposition Break the system into smaller units

Programming languagesDefine abstractions for the small unitsCompose the units into systems

Page 4: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 5: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

519.12.2006

Modularity

Abstraction Decomposition Encapsulation Information Hiding Separation of Concerns

Low couplingHigh cohesion

Page 6: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

619.12.2006

Concerns

Particular goal, concept, or area of interest Primary motivation for organizing and

decomposing software into manageable and comprehensible parts

Page 7: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

719.12.2006

Separation of Concerns

CohesionMaximize cohesion within a component

CouplingMinimize coupling between components

Page 8: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

819.12.2006

Separation of Concerns

AdvantagesUnderstandabilityReusabilityExtensibilityMaintainabilityAdaptability

Page 9: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

919.12.2006

The Problem

Limitations of OOPdecomposition resulting in cross-cutting

concerns Insufficieny of OO and procedural

development techniques

Page 10: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 11: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 12: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

1219.12.2006

Crosscutting Concern Examples

Display

Figure FigureElement*

Point LinegetX()

getY()

getP1

setP1

DisplayTrackingsetX(int)

setY(int)

setP1(Point)

setP2(Point)

2

Page 13: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

1319.12.2006

Cost of Crosscutting Concerns

Reduced understandability Decreased adaptability Decreased reusability

Decreased maintainability

Page 14: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

1419.12.2006

Component

Cleanly encapsulated in a generalized procedure (i.e. object, method, procedure, API)

Well-localized, and easily accessed and composed

Page 15: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

1519.12.2006

Aspect

Properties that affect the performance or semantics of the components in systemic ways

Not cleanly encapsulated in a generalized procedure

Page 16: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 17: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

1719.12.2006

AOP

A promising new technology for separating crosscutting concerns that are usually hard to do in object-oriented programming

Page 18: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

1819.12.2006

Fundamentals of AOP

Aspect as a modularization unit Three distinct development steps:

Aspectual decomposition Concern implementation Aspectual recomposition

Page 19: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

1919.12.2006

Steps of AOP

Page 20: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2019.12.2006

Programming with Aspects

Writing the components Writing the aspects Weaving

Page 21: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2119.12.2006

Aspect Weaving

Page 22: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2219.12.2006

Tools for AOP

AspectJ AspectC++ AspectWerkz JAC JBoss-AOP Nanning

Page 23: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 24: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2419.12.2006

AspectJ Terminology

Join point Pointcut Advice Introduction Aspect

Page 25: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2519.12.2006

Join Points

Well-defined points in a program's execution

Key points in dynamic call graph

Page 26: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2619.12.2006

Pointcuts

A named collection of join points Designate join points

Page 27: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2719.12.2006

Advice

Before advice After advice Around advice

Page 28: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2819.12.2006

Introduction

ModifiesMembers of a classRelationship between classes

Page 29: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

2919.12.2006

Aspect

Module for handling crosscutting concernsDefined in terms of pointcuts, advice,

and introductionReusable and inheritable

Page 30: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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); }}

Page 31: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 32: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

3219.12.2006

Advantages of AOP

TractabilitySimpler cleaner codeReusabilityEasier to maintain

Page 33: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 34: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

3419.12.2006

Conclusions

Scattered crosscutting concerns over several modules causing tangling code

Explicit abstraction mechanism with AOP Increased modularity of the system

Page 35: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

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

Page 36: 19.12.2006 1 Aspect Oriented Programming Gülşah KARADUMAN

3619.12.2006

Thank you for your attention!

Questions?