Unit 3 : Aspect Oriented Programming

          In Aspect oriented programming, we break program logic into the so called concerns. This paradigm can mainly be used when there is a single requirement that has to be implemented in multiple modules/components. The key benefit of Aspect Oriented Programming is they provide separation of concerns. That means each element(method, class) in a program does one thing and only one thing. Concerns might be different to different stakeholders, it is not right to assume that all the stakeholders of particular system can have same concern. Hence, it can be defined as “Something that is of interest or significance to a stakeholder or a group of stakeholders.”
These concerns can be divided into core concerns and cross-cutting concerns.

  1. Core concerns             : These are the functional concerns that relate to its primary purpose.
  2. Cross-cutting concerns : These are the concerns which effect other concerns. These can not be clearly decomposed from the rest of the system. The cross-cutting concerns give rise to tangling and scattering. Tangling occurs when a module in a system included some code that implements different system requirements.

           The aim of Aspect oriented programming is to encapsulate(group) these cross cutting concerns into a single aspect. Hence, aspect is a common feature in a system that is scattered across methods, classes etc..The aspect also tells where the requirement it describes has to be included in the system i.e. the location where the particular cross cutting concern has to be executed in the program.

Technical terminologies:
Below are certain terminologies that are commonly used and are basics to understand the aspect oriented programming.
Advice            : The code implementing a concern.
Aspect            : A program abstraction that defines a cross-cutting concern. It includes the definition of a pointcut and the advice associated with that concern.
Join Point        : An event in an executing program where the advice associated with an aspect may be executed.
Pointcut          : A statement, included in the aspect, that defines the join points where the associated aspect advice should be executed.
Weaving         : The incorporation of advice code at the specified join point by an aspect weaver.
Aspect Weaver: These are extensions to compilers that process the definition of aspects and the object classes and methods that define the system.

Consider an example:
               A medical records system such as the MHC-PMS includes components that handle logically related patient information. The patient component maintains personal information about a patient, the medication component holds information about medications that may be prescribed, and so on. This organization requires that information in the database has to be updated from a number of different places in the system. For example, patient information may be modified when their personal details change, when their assigned medication changes, when they are assigned to a new specialist, etc. For simplicity consider all the database update methods begin with update* keyword.
                                    updatePersonalInformation (patientId, infoupdate)
                                    updateMedication (patientId, medicationupdate)

All the updates are made by hospital staff, who are logged into the system. Suppose a security breach occurs , i.e., a hospital staff accidentally left his/her system logged on and an unauthorized person makes some malicious changes to patient database. In order to avoid this the organization might decide to include some security features such as reauthenticating the use before making any updates to database and logging the details of who made made the changes in a separate file.

  • One way of implementing this new policy is to modify the update method in each component to call other methods to do the authentication and logging. This will result in tangled implementation as the authentication and logging are two different concerns, these might be implemented at different places in the system.
  • Alternatively, the system could be modified so that each time an update method is called, method calls are added before the call to do the authentication, and then after to log the changes made. This will result in scattered implementation as the same code has to be included at several different places in the system.

Here, authentication and logging from the cross-cutting concerns which cut across the core concerns of the system and may have to be included in several different places. Hence these can be written as two separate aspects in AOP.
aspect authentication
{
before: call (public void update* (..)) // this is a pointcut
{
// this is the advice that should be executed when woven into the executing system
int tries = 0 ;
string userPassword = Password.Get ( tries ) ;
while (tries < 3 && userPassword != thisUser.password ( ) )
{
// allow 3 tries to get the password right
tries = tries + 1 ;
userPassword = Password.Get ( tries ) ;
}
if (userPassword != thisUser.password ( )) then
//if password wrong, assume user has forgotten to logout
System.Logout (thisUser.uid) ;
}
} // authentication

In this example,
Pointcut : before: call (public void update* (..))
              Tells before executing every update method the advice, for reauthenticating, in the aspect has to be executed.
Advice   : In this case, the advice gets a password from the person requesting the change and checks that it matches the password of the currently logged-in user. If not, the user is logged out and the update does not proceed.
Weaving: The inclusion of advice at the join points specified in the pointcuts is the responsibility of an aspect weaver. This is shown in below diagram. The weaver then generates a new program with the aspects included at the specified join points such that the cross-cutting concerns are executed at the right places in the final system.

Aspect Weaving

                     Aspect Weaving

There are three different approaches to aspect weaving:
1. Source code pre-processing, where a weaver takes source code input and generates new source code in a language such as Java or C++, which can then be compiled using the standard language compiler.
2. Link time weaving, where the compiler is modified to include an aspect weaver.
3. Dynamic weaving at execution time. In this case, join points are monitored and when an event that is referenced in a pointcut occurs, the corresponding advice is integrated with the executing program.
             The most commonly used approach to aspect weaving is link time weaving, as this allows for the efficient implementation of aspects without a large run-time overhead. Dynamic weaving is the most flexible approach but can incur significant performance penalties during program execution. Source code pre-processing is now rarely used.

Advertisements

One thought on “Unit 3 : Aspect Oriented Programming

  1. Smitha Iddalgave April 21, 2013 at 1:08 pm Reply

    Reblogged this on smitaiddal87.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: