180 likes | 289 Views
Aspect Oriented Programming. Written by Michael Beder. Agenda. Why Aspects? Examples Class Invariants Debugging Basic Terminology AspectJ Question. Why Aspects?. Some design and implementation problems are not related to a specific class or module.
E N D
Aspect Oriented Programming Written by Michael Beder
Agenda • Why Aspects? • Examples • Class Invariants • Debugging • Basic Terminology • AspectJ • Question Aspect Oriented Programming
Why Aspects? • Some design and implementation problems are not related to a specific class or module. • Asserting (Invariants, Parameters, Pre/Post conditions). • Debugging. • Logging. • Security. • Robustness. • Such problems relate to crosscutting concerns of the software. • May originate in Non-Functional requirements. • Solving them using regular OOP methods leads to scattering and tangling. • An Aspect solution offers a uniform treatment of the problem. Aspect Oriented Programming
Crosscutting concerns Packages Security Logging Asserting Robustness Aspect Oriented Programming
Case Study: Class Invariants Class Rectangle { private Point center; private int width, height; public move(Point to) {…} public setDimensions(int width, int height) {…} public draw() {…} } Class Invariant: (width > 0) && (height > 0) && (center.x > 0) && (center.y > 0). We want to assert the fulfillment of the invariant. How to do it? Aspect Oriented Programming
Solution I Class Rectangle { private Point center; private int width, height; public move(Point to) {if (width > 0) && …} public setDimensions(int width, int height) {if (width > 0) … } public draw() {if (width > 0) … } } • Code multiplication. • What if the invariance changes? • Hard to maintain. • Only disadvantages. Aspect Oriented Programming
Solution II Class Rectangle { private Point center; private int width, height; private checkInvariant() {if (width > 0) && …} public move(Point to) {checkInvariant() …} public setDimensions(int width, int height) {checkInvariant() …} public draw() {checkInvariant() …} } • Code multiplication! • Adding additional methods? • Still not elegant and still scattered. What limits us? Can we do better? Aspect Oriented Programming
A Moment of Truth • Invariant fulfillment is not a part of the functionality of Rectangle. • It is a higher property derived from the functionality. • Asserting is performed in places that have a common pattern. • At the beginning of each method. • The execution mechanism forces us to scatter the responsible code among the methods. Why not use another execution mechanism? Applying the scattering pattern in a uniform way Aspect Oriented Programming
Basic Terminology • A Join Pointis a well-defined event in the execution of the program. • e.g. a specific method call, a specific block execution, handling a specific exception, etc. • May be or may be not realized in a specific program execution. • Has a dynamic context and identity. a.f() … while (…) { … } … a.f() join point 1 Is join point 1 is the same as joint point 3 ? Program execution join point 2 join point 3 Aspect Oriented Programming
Basic Terminology – cont. • A Pointcutis a syntactic construct that defines a set of join points. • e.g. a method call in the code, the method itself (all calls), change of some variable. a.f() … while (…) { … } … a.f() join point 1 pointcut 1: “call to method f” Program execution join point 2 join point 3 a syntactic construct Aspect Oriented Programming
Basic Terminology – cont. • An Adviceis a connection between a pointcut and an action. • before advice: action to be done right before the program proceeds to a join point defined by the pointcut. • after advice: action to be done right after the program leaves a join point defined by the pointcut. Define the scattering pattern as a pointcut Implement the solution as an advice on the pointcut Aspect Oriented Programming
AspectJ • General-purpose, aspect-oriented extension to Java. • Every valid Java program is a valid AspectJ program. • May 25, 2004 – AspectJ 1.2 released. • AspectJ enables definition of join points, pointcuts advices and more. • General workflow: • Implementing the core concerns using Java. • Implementing the crosscutting concerns using AspectJ extension. • Weaving both implementations using ajc (no need for javac). Java Code AspectJ Weaver Java object code Compiler AspectJ Code Aspect Oriented Programming
Solution III: Using AspectJ Rectangle’s code is not changed Class Rectangle { private Point center; private int width, height; public move(Point to) {…} public setDimensions(int width, int height) {…} public draw() {…} } pointcut checkInvariance(Rectangle rect) : call(* Rectangle.*(..)) && target(rect); before(Rectangle rect) : checkInvariance(rect) { if (rect.width > 0) && (rect.height > 0) && … } Corresponds to the scattering pattern problem specific implementation ‘before’ advice Aspect Oriented Programming
Case Study: Debugging Class Rectangle { private Point center; private int width, height; public move(Point to) {…} public setDimensions(int width, int height) {…} public draw() {…} } Suppose we want to trace each change of height in move(). How to do it? • Print height before each use? • Use setHeight(), getHeight() only? • works, but too radical. • An Aspect solution! Aspect Oriented Programming
Debugging – cont. Class Rectangle { private Point center; private int width, height; public void move(Point to) {…} public void setDimensions(int width, int height) {…} public void draw() {…} } pointcut heightInMove(Rectangle rect, int newHeight) : execution(public void Rectangle.move(Point)) && target(rect) && set(private Rectangle.height) && args(newHeight); after(Rectangle rect, int newHeight) : heightInMove(rect, newHeight) { System.out.println(“Height of Rectangle ” + rect.toString() + “ is changed to “ + newHeight); } ‘after’ advice Aspect Oriented Programming
Solution Advantages • No scattering. • The responsible code is concentrated in one place. • No tangling. • Suppose we want to build a stack of all height’s values. Its definition and update will be concentrated in one place. • The debugged class remains unchanged. • Reduces the chance of creating new bugs while debugging. • Changes are made easily. • Tracing height before its change. • Tracing every change of height. Aspect Oriented Programming
Question (from exam) • After implementing the product, the developers of ABC inc. company realized that they forgot to implement logging of every action. One of the developers suggested to make the change in the following way: For each class Foo create a derived class FooWithLog that adds logging: FooWithLog int action () { log(“starting action”); int value = super.action(); log(“ending action”); return value; } Foo int action () { // do stuff } Aspect Oriented Programming
Question (from exam) – cont. 1. What are the main advantages and disadvantages of this solution? 2. Describe a better solution using the Abstract Factory Pattern. Draw the class diagram. 3. Describe another solution using different language/mechanism. Compare this solution to the previous solution. Aspect Oriented Programming