Suppose your manager comes a day before you are going to ship the code to customer and asks you to put a logging code at each method calls exit and entry. There are couple of hundred class files and each class file on an average twenty methods. What you do? Go for a long vacation, if possible. These are the kinds of problem that AOP aims to solve.
AOP stands for Aspect Oriented Programming. AOP is not a a supplement or replacement of OO Programming. Rather it compliments with OO programming. When we develop an application,
usually we deal with two kinds of concerns in building application, which are:
usually we deal with two kinds of concerns in building application, which are:
- Business Concern : What is the domain model and the important actors in the system. For example if you are building a web application to handle school you might have classes like Students, Subjects, Teachers etc. Students and Teachers might be sitting in an inheritance hierarchy. Then there is association between Student and Teacher, Student and Subject. These problems are well suited for OO programming paradigm.
- Cross cutting Concern: Any non trivial application has to have many other aspects in place to handle real life situation. Logging to monitor the dynamic behaviour of application. Security to stop unwarranted access to application. Transactions so that certain interactions have acid behaviour. How to handle these issues is usually independent of your domain. A common thread can be traced around these discussions for any application development team. This is where AOP comes and fit into the picture.
The advantage that AOP brings on table is that
- Concerns are placed at one place. For example if you have to put logging on entry and exit of every method you will have to write some logging code at the beginning and end of each method. However with AOP
- Keeps the code neat and clean: The code basically pertains to business logic.The cross cutting concerns are placed at a common location handled by AOP frameworks.
Disadvantages of using AOP:
- Code Inspection and reviews will be difficult - At runtime the behaviour of code might change because of the AOP applied to it.
- Testing infrastructure need- Again this comes from first disadvantage. The code at development time has a different behaviour at runtime. Even unit testing has to be done in such a way that the impact of AOP at runtime is taken care off.
- Coding discipline - AOP requires a lot of coding discipline especially in terms of naming. As AOP depends a lot on these to apply Advices.
- Architecture representation - Architectural representations are pretty mature when it comes to OO based architectures. However AOP representations still has to evolve.
AOP comes with certain terminology which is standard across various AOP frameworks:
- Advice: Advice is the piece of code that needs of executed to apply AOP. Various kinds of advice are like before method executing, after method execution.
- Joinpoint: Joinpoint is a point during the execution of your application. A joinpoint can be a method call or construction of an object.
- Pointcut: Pointcut usually is an expression which tells where all advice need to be applied. Like which all methods or which all classes the advice should be applied.
- Aspect: Aspect is a combination of Advice and Pointcut. For example call a advice before all the method calls in a particular class constitutes an Aspect.
- Weaving: This is the process of putting the aspect together with the application code to give the desire behaviour.There are two prominent kind of weaving. One is static and another is dynamic. In Static the class definitions itself is modified to bring the desire behaviour. In dynamic weaving, the behaviour of the class is modified either by modifying at load time or by putting a proxy.
- Target: Target is the object on whom the advised is applied. Usually these are your application domain classes.
- Introduction: Introduction is about introducing new methods or fields in a class at runtime. Also the object may implement new interfaces at runtime. Please be careful that these all are runtime behaviour modifications and the class definition still remains same.
AOP is also differentiated in terms of if it the behaviour modification is achieved statically or dynamically.
- Static AOP: In static AOP the class definition of the class (bytecode) itself is modified to bring the AOP into effect. This brings performance at the cost of flexibility . If you have change the behaviour or introduce new behaviour the classes need to be recompiled.
- Dynamic AOP: In dynamic AOP the behaviour of the class is modified at runtime. This is either done by modifying the class definition while loading the class or using proxies.It is highly flexible because to change the behaviour the recompilation of the target classes is not required but this comes at the cost of performance.
No comments:
Post a Comment