A Brief Introduction to Aspect-Oriented ProgrammingHistorical View Of LanguagesProcedural LanguageFunctional LanguageObject-Oriented ProgrammingOOP (cont’d)We Need MoreWe Need More (cont’d)Basic Concepts in AOPAspectJ by ExampleAspectJ by Example (cont’d)PointcutsAdviceAspectsIntroductionIntroduction (cont’d)Slide 17Slide 18What Can AspectJ Do for UsConclusion and Open IssuesRelated WorkA Brief Introduction to Aspect-Oriented ProgrammingZhenxiao YangHistorical View Of LanguagesProcedural languageFunctional languageObject-Oriented languageProcedural LanguageAlso named imperative languageDescribeAn explicit sequence of steps to follow to produce a resultExamples: Basic, Pascal, CFunctional LanguageAlso named declarative languageDescribe relationships between variables in terms of functions or inference rules.ExamplesProlog: Logical programming languageHaskell: Functional languageObject-Oriented ProgrammingDescribe A set of objects And communications among them to produce a resultBasic featuresEncapsulationInheritancePolymorphismOOP (cont’d)Example languagesFirst OOP language: SIMULA-67 (1970)Smalltalk, C++, JavaMany other:Ada, Object Pascal, Objective C, DRAGOON, BETA, Emerald, POOL, Eiffel, Self, Oblog, ESP, POLKA, Loops, Perl, VBAre OOP languages procedural?We Need MoreMajor advantage of OOPModular structurePotential problems with OOPIssues distributed in different modules result in tangled code.Example: loggingWe Need More (cont’d)We need a solution to this tangled-code problemOne approach is Aspect-Oriented ProgrammingBasic Concepts in AOPcross-cuttingaspectjoin pointpointcutadviceintroductionAspectJ by ExampleAspectJ by Example (cont’d)Define pointcutsDefine adviceIntroductionPointcutspointcutpointcut 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));AdviceAdviceafter(): 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); }Aspectsaspect 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 + ")."); } }IntroductionIntroductionAdd members to a set of ClassesChange inheritance structure of classesIntroduction (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); } ....}Introduction (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;}Introduction (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;}What Can AspectJ Do for UsDevelopingTracing, Logging, and ProfilingPre- and Post-ConditionsContract EnforcementProductionChange MonitoringSynchronizationContext PassingProviding Consistent BehaviorIntroductionConclusion and Open IssuesAOP vs OOPAOP is not substitute for OOPAOP makes OOP more powerfulAOP also has runtime overheadWe should use OOP as much as possibleReuse of aspectsTool supportRelated WorkAspectJReflection and meta-object protocolsMeta-object provides mechanism to control over base-objectsSubject-Oriented ProgrammingIntentional
View Full Document