A Brief Introduction to AspectOriented Programming Zhenxiao Yang
A Brief Introduction to Aspect-Oriented Programming Zhenxiao Yang
Historical View Of Languages n n n Procedural language Functional language Object-Oriented language
Procedural Language n n Also named imperative language Describe n n An explicit sequence of steps to follow to produce a result Examples: Basic, Pascal, C
Functional Language Also named declarative language Describe n n n relationships between variables in terms of functions or inference rules. Examples n n n Prolog: Logical programming language Haskell: Functional language
Object-Oriented Programming n Describe n n n A set of objects And communications among them to produce a result Basic features n n n Encapsulation Inheritance Polymorphism
OOP (cont’d) n Example languages n n n First OOP language: SIMULA-67 (1970) Smalltalk, C++, Java Many other: n n Ada, Object Pascal, Objective C, DRAGOON, BETA, Emerald, POOL, Eiffel, Self, Oblog, ESP, POLKA, Loops, Perl, VB Are OOP languages procedural?
We Need More n The nice things about OOP n n Modular structure The problems of OOP n n Issues distributed in different modules result in tangled code. Example: logging
We Need More (cont’d) n We need a solution to this tangled-code problem n One of the sound answer is Aspect. Oriented Programming
Basic Concepts in AOP n n n cross-cut aspect joint pointcut advice introduction
Aspect. J by Example
Aspect. J by Example (cont’d) n n n Define pointcuts Define advice Introduction
Pointcuts n pointcut n n n pointcut move(): call(void Figure. Element. set. XY(int, int)) || call(void Point. set. X(int)) || call(void Point. set. Y(int)) || call(void Line. set. P 1(Point)) || call(void Line. set. P 2(Point)); pointcut produce call(void Figure. make*(. . )) pointcut set. XY(Figure. Element fe, int x, int y): call(void fe. set. XY(x, y));
Advices n Advice n n after(): move(){ System. out. println(“A figure element moved. ”); } after (Figure. Element fe, int x, int y): set. XY(fe, x, y){ System. out. println(fe + “ moved to ” +x, + “ , ” + y); }
Aspects aspect Figure. Log{ pointcut set. XY(Figure. Element fe, int x, int y): calls(void fe. set. XY(x, y)); after(Figure. Element fe, int x, int y): set. XY(fe, x, y){ System. out. println(fe + " moved to (" + x + ", " + y + "). "); } }
Introduction n n Add members to a set of Classes Change inheritance structure of classes
Introduction (cont’d) aspect Point. Observing { private Vector Point. observers = new Vector(); public static void add. Observer(Point p, Screen s) { p. observers. add(s); } public static void remove. Observer(Point p, Screen s) { p. observers. remove(s); }. . }
Introduction (cont’d) public class A 1{ function foo(){…} } public class A 2{ function foo(){…; super. foo(); …} } public class A 3{ function foo(){…; super. foo(); …} } aspect A 1 A 2 A 3{ declare parents: A 2 extends A 1; declare parents: A 3 extends A 2; }
Introduction (cont’d) public class A 1{ function foo(){…} } public class A 2{ function foo(){…; super. foo(); …} } public class A 3{ function foo(){…; super. foo(); …} } aspect A 1 A 3 A 2{ declare parents: A 3 extends A 1; declare parents: A 2 extends A 3; }
What Can Aspect. J Do for Us n Developing n n Production n n Tracing, Logging, and Profiling Pre- and Post-Conditions Contract Enforcement Change Monitoring Synchronization Context Passing Providing Consistent Behavior Introduction
Conclusion and Open Issues n AOP vs OOP n n n AOP also has runtime overhead n n n AOP is not substitute of OOP AOP makes OOP more powerful We should use OOP as much as possible Reuse of aspects Tool support
Related Work n n Aspect. J Reflection and meta-object protocols n n Meta-object provides mechanism to control over base-objects Subject-Oriented Programming n
Thank You!
- Slides: 22