Aspect Oriented Programming Tutorial (Part-3) Aasim Naseem, January 1, 2010 Hello All. hope that everything is going best in your life. This is a third part of my tutorial on topic of Aspect Oriented Programming … its too late .. i know … a month or more!! … second part was posted i think 35 40 days back … and after that i got too much busy in some projects … plus my notebook had some problem with display hardware. Though i have composed this part many days back but final draft wasn’t compliled till today …. the thread of black berry application tutorial is also waiting for next part … seems i’m too much lazy … Anyhow.. here are some more concepts regarding AOP. Due to shortage of time i will not include programing example today … the next part will cover all previous theory with practical examples … today just grasp few more concepts … In this part, you will know some of basic terms/concepts that are core of AOP. You have to keep these things in considerations while building your application with AOP. let have a look over them … Aspects in very black-box definition, aspects in AOP are like classes in OOP. we put all the code that has scattered across the application in single unit call aspect, or in other words we express cross-cutting concerns in stand-alone modules called aspect (first benefit of AOP that it collects all scattered or tangled code across the application in form of cross cutting concerns and put them in separate units). For example, as we discussed in last part, logging code can cross-cut many modules, yet the aspect of logging should be separate from the functional concerns of the module where logging code was written. Aspects are similar to classes in their declaration, and they define types, just as classes do. In addition to its declaration, aspects contains both point-cuts and advice. We will define these two concerns in next sections. Joint Points A join point could be any point in the flow of your program. As name depicts this is a point in our application or source code where our program and aspect meet each other. or in other worlds, this is the point where AOP play its role. Join points represent well-defined points in a program’s execution that could be any method/constructor calls, method/constructor execution, field get and set, exception handler execution and/or static and dynamic initialization. You told your compiler via joint points that at this point inject the code from aspects and make final executable program. Advices Advices holds those piece of codes that actually executes when AOP play its role in our program. It may contain code that scattered across the application. Advice is a piece of code that we inject in our program when a join point is reached. You define advice relative to a point-cut like advices can executes before, after, or around a join point. It is an additional code that runs along with execution of our running program. Again talk about logging.It is a concern that cross cut across the application, means same code for logging can be found in various modules of application. To understand the concept of advices, just think that we first define joint points in our application where we need logging. it means we find those methods where same logging code is present. secondly we put all logging code (infact same piece of code) in separate pieces of code called “advice” and then while compilation of our program, compiler inject advice’s code when its join point is reached. By doing that we picked all occurrence of same piece of code and put it in separate entity. Don’t be confuse between advice and aspect. To understand the difference just think aspect like class and advice as any method in that class. got it …. !!!! Point cut Simply a pointcut is a set of join points. Whenever the program execution reaches one of the join points described in the pointcut, a piece of code associated with the pointcut (called advice) is executed. This allows a programmer to describe where and when additional code should be executed in addition to an already defined behavior. This permits the addition of aspects to existing software. Introduction I love this feature of AOP. Believe me if you get real soul of this, you will become fan of AOP, like i’m. Introduction allows aspects to modify the static structure of a program. By using introduction, aspects can add new methods and variables to a class, declare that a class implements an interface, or convert checked to unchecked exceptions. To understand what we can get by using Introductions, consider the following real life scenario. In every institute, student has to pay their fee as monthly or semester basis. A student gets its fee slip form accounts branch and submits it in bank and get receipt. His/her account file has been updated with copy of fee receipt. Thats looks fine in such scenario. What if a student unable to submit his/her fee till dead line??? he/she has to submit a request form for granting him extension in fee submission or even for allowing him/her to submit his late fee. In such case accounts branch will issue him an separate fee slip mentioning late fee fine. He/she will submit his late fee and will get fee receipt. In his record, few additional documents will be filed .i.e. application for allowing late fee, a late fee submission receipt, some other documents (like medical certificate etc) to prove why he/she was unable to submit fee within time. In both cases, different kind of data was filed though the process was same .i.e. submitting fee. Now let try to automate this scenario as a software module. You need to create a class having basic attributes related to fee like paymentDate, amount. The rest of the attribute that you need to track late fee submission will be stored in an aspect. Incase of any late fee case, the aspect will inject (or introduce) additional variable/methods into existing class so that you can store and retrieve information as per scenario. Remember that class structure will not change, just a new version or object will have some additional attribute. Hence different objects of same class may have different kind of attribute due to introduction feature of aspects. Aspect Weaver It weave our aspect and other business logic into single/final executable program. It works at compilation level of source code. How does it work is beyond the discussion of this tutorial. I will write a short note about its core functionality later on. Right now just think with AOP, we start by implementing our project using our OO language (for example, Java, or C++ ), and then we deal separately with crosscutting concerns in our code by implementing aspects. Finally, both the code and aspects are combined into a final executable form using an aspect weaver. As a result, a single aspect can contribute to the implementation of a number of methods, modules, or objects, increasing both reusability and maintainability of the code. We just define join-points, point-cuts, advices and aspects along with our business logic of program and then aspect weaver merge (infact weave) these things in our source code at compilation time. in conventional way, our source code is simply compiled by language compiler and produce final executable code. Aspect Weaver first weave your aspects, point-cuts, join-points, advices into our source code and make final script of code that will be ready to be compiled by language compiler, hence producing executable program containing our business logic along with AOP features. Please follow and like us: Aasim NaseemI’m an engineer by profession, a blogger and a photojournalist by hobby. Seasonal writer at LAFZ Media UK. I write on different topics and things around us. Contact me at +971.56.126.8842 or Aasim.Naseem@outlook.com Aspect Oriented Programming Tips & Tutorials AdvicesAspect Oriented Programming TutorialAspect Oriented Programming Tutorial (Part-3)Aspect WeaverAspectsJoint PointsPoint Cuts
Hi Ans…..you have a wonderful writing style….I really enjoyed reading your 3 parts in just 1 sitting and everything went inside my head easily, I really appreciate how nicely you have explained the concepts. I am looking forward to a part 4 Thank you very much Reply
@ kanaka thank you very much to find this much time for reading my tutorial … just little bit busy in some project else i will definitely post its next part. thanks again and please give your kind suggestions to make it more better. Reply
have read all three tutorial’s.Though i have heard about the term but never consider to dig it.After reading your blog i really got the concept of AOP.it would be really great to see some programming example. and thanks for an excellent tutorial. 🙂 Reply