Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aspect Oriented Programming Written by Michael Beder.

Similar presentations


Presentation on theme: "Aspect Oriented Programming Written by Michael Beder."— Presentation transcript:

1 Aspect Oriented Programming Written by Michael Beder

2 Aspect Oriented Programming2 Agenda Why Aspects? Examples –Class Invariants –Debugging Basic Terminology AspectJ Question

3 Aspect Oriented Programming3 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.

4 Aspect Oriented Programming4 Crosscutting concerns Packages Security Logging Asserting Robustness

5 Aspect Oriented Programming5 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?

6 Aspect Oriented Programming6 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.

7 Aspect Oriented Programming7 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?

8 Aspect Oriented Programming8 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

9 Aspect Oriented Programming9 Basic Terminology A Join Point is 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() Program execution join point 1 join point 2 join point 3 Is join point 1 is the same as joint point 3 ?

10 Aspect Oriented Programming10 Basic Terminology – cont. A Pointcut is 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() Program execution join point 1 join point 2 join point 3 pointcut 1: “call to method f” a syntactic construct

11 Aspect Oriented Programming11 Basic Terminology – cont. An Advice is 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

12 Aspect Oriented Programming12 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). Compiler Java Code AspectJ Code Java object code AspectJ Weaver

13 Aspect Oriented Programming13 Solution III: Using AspectJ 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) && … } Rectangle’s code is not changed Corresponds to the scattering pattern problem specific implementation ‘before’ advice

14 Aspect Oriented Programming14 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!

15 Aspect Oriented Programming15 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

16 Aspect Oriented Programming16 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.

17 Aspect Oriented Programming17 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: Foo int action () { // do stuff } FooWithLog int action () { log(“starting action”); int value = super.action(); log(“ending action”); return value; }

18 Aspect Oriented Programming18 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.


Download ppt "Aspect Oriented Programming Written by Michael Beder."

Similar presentations


Ads by Google