r r r a brief introduction to aspect-oriented programming
Post on 15-Jan-2016
242 views
TRANSCRIPT
![Page 1: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/1.jpg)
R
R
R
A Brief Introduction to Aspect-Oriented Programming
![Page 2: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/2.jpg)
R
R
RHistorical View Of Languages
• Procedural language
• Functional language
• Object-Oriented language
![Page 3: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/3.jpg)
R
R
RAcknowledgements
• Zhenxiao Yang
• Gregor Kiczales
![Page 4: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/4.jpg)
R
R
RProcedural Language
• Also termed imperative language
• Describe– An explicit sequence of steps to follow to
produce a result
• Examples: Basic, Pascal, C, Fortran
![Page 5: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/5.jpg)
R
R
RFunctional Language
• Describe everything as a function (e.g., data, operations)
• (+ 3 4); (add (prod 4 5) 3)
• Examples– LISP, Scheme, ML, Haskell
![Page 6: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/6.jpg)
R
R
RLogical Language
• Also termed declarative language
• Establish causal relationships between terms– Conclusion :- Conditions– Read as: If Conditions then Conclusion
• Examples: Prolog, Parlog
![Page 7: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/7.jpg)
R
R
RObject-Oriented Programming
• Describe – A set of user-defined objects – And communications among them to
produce a (user-defined) result
• Basic features– Encapsulation– Inheritance– Polymorphism
![Page 8: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/8.jpg)
R
R
ROOP (cont’d)
• Example languages– First OOP language: SIMULA-67 (1970)– Smalltalk, C++, Java– Many other:
• Ada, Object Pascal, Objective C, DRAGOON, BETA, Emerald, POOL, Eiffel, Self, Oblog, ESP, POLKA, Loops, Perl, VB
• Are OOP languages procedural?
![Page 9: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/9.jpg)
R
R
RWe Need More
• Major advantage of OOP– Modular structure
• Potential problems with OOP– Issues distributed in different modules result in
tangled code.– Example: error logging, failure handling,
performance optimizations
• Potential result: Tangled Code– Change in the way logging is done affects many
classes
![Page 10: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/10.jpg)
R
R
RExample of Tangled Code
Red indicates the error-logging code
![Page 11: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/11.jpg)
R
R
RUntangling the Tangled Code
• Constraint:– Want to preserve the benefits of OOP
(encapsulation, modularity, inheritance, etc.)
• Potential Solution:– Aspect-Oriented Programming
![Page 12: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/12.jpg)
R
R
RBasic Concepts in AOP
• Crosscutting: straddle across functional and hierarchical boundaries
• Aspect: – Property cannot be cleanly encapsulated
into a single procedure– Tend to affect performance or semantics of
components in systematic ways
![Page 13: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/13.jpg)
R
R
RAOP: Languages
• Components:– Component program– Aspect definition– Aspect Weaver
• Constructs:– Join point: execution point in component program for integrating
aspects– Pointcuts: refers to collection of join points and values of variables
at those points– Advice: method-like constructs that define additional behavior at
join points– Aspects: “units of modular cross-cutting implementation”
• Pointcuts• Advice• Regular (Java) code
![Page 14: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/14.jpg)
R
R
RPictoral Representation
Weaver
Executable
Aspect
Compiler
Compiler
Executable
![Page 15: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/15.jpg)
R
R
RAspectJ by Example
![Page 16: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/16.jpg)
R
R
RAspectJ by Example (cont’d)
• Define pointcuts
• Define advice
• Introduction
![Page 17: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/17.jpg)
R
R
RPointcuts
• pointcut– pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point)); – pointcut produce call(void Figure.make*(..))– pointcut setXY(FigureElement fe, int x, int y): call(void fe.setXY(x, y));
![Page 18: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/18.jpg)
R
R
RAdvice
• Advice– after(): move(){ System.out.println(“A figure element
moved. ”); } – after (FigureElement fe, int x, int
y):setXY(fe,x,y){ System.out.println(fe + “ moved to ”
+x,+ “ , ” + y); }
![Page 19: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/19.jpg)
R
R
RAspects
aspect FigureLog{
pointcut setXY(FigureElement fe, int x, int y):
calls(void fe.setXY(x, y));
after(FigureElement fe, int x, int y): setXY(fe, x, y){
System.out.println(fe + " moved to ("
+ x + ", " + y + ").");
}
}
![Page 20: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/20.jpg)
R
R
RIntroduction
• Introduction– Add members to a set of Classes– Change inheritance structure of classes
![Page 21: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/21.jpg)
R
R
RIntroduction (cont’d)
aspect PointObserving { private Vector Point.observers = new Vector();
public static void addObserver(Point p, Screen s) { p.observers.add(s); }
public static void removeObserver(Point p, Screen s) { p.observers.remove(s); } ....}
![Page 22: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/22.jpg)
R
R
RIntroduction (cont’d)
public class A1{ function foo(){…}}public class A2{ function foo(){…;super.foo();…}}public class A3{ function foo(){…;super.foo();…}}aspect A1A2A3{ declare parents: A2 extends A1; declare parents: A3 extends A2;}
![Page 23: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/23.jpg)
R
R
RIntroduction (cont’d)
public class A1{ function foo(){…}}public class A2{ function foo(){…;super.foo();…}}public class A3{ function foo(){…;super.foo();…}}aspect A1A3A2{ declare parents: A3 extends A1; declare parents: A2 extends A3;}
![Page 24: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/24.jpg)
R
R
RWhat Can AspectJ Do for Us
• Developing– Tracing, Logging, and Profiling– Pre- and Post-Conditions– Contract Enforcement
• Production– Change Monitoring– Synchronization– Context Passing– Providing Consistent Behavior
• Introduction
![Page 25: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/25.jpg)
R
R
RConclusion and Open Issues
• AOP vs OOP– AOP is not substitute for OOP– AOP makes OOP more powerful
• AOP also has runtime overhead– We should use OOP as much as possible
• Reuse of aspects
• Tool support
![Page 26: R R R A Brief Introduction to Aspect-Oriented Programming](https://reader036.vdocuments.site/reader036/viewer/2022081503/56649d545503460f94a31c50/html5/thumbnails/26.jpg)
R
R
RRelated Work
• AspectJ
• Reflection and meta-object protocols– Meta-object provides mechanism to control over base-objects
• Subject-Oriented Programming
• Intentional Programming