Aspect Oriented Programming

Aspect Oriented Programming

Table of Contents

Here an aspect that is a common feature – typically distributed allover methods, classes or may be even entire object models. It is behaviour that looks and smells like it should have proper body, but cannot find a way to express this structure in the code with an traditional object-oriented ways.

For example, metrics is common aspect. To create useful logs from the application we have often to sprinkle informative messages throughout the code. Metrics is something that class or object model is really shouldn’t be concerned.

In AOP, a feature that metrics is called crosscutting concern, as the behaviour that will“cut” across multiple points in our objet models. As a development technique, AOP recommends that us abstract and encapsulate crosscutting concerns.

Consider an example, to add any code to an application to measure the amount of time it would take to invoke a particular method like

public class BankAccountAWQ

{

  public void withdraw(double amt)

  {

    long strtTime = System.currentTimeMillis();

    try

    {

      // Actual method body…

    }

    finally

    {

      long endtime = System.currentTimeMillis() – strtTime;

      System.out.println(“withdraw took: ” + endtime);

    }

  }

When this code works there will be few problems with the approach:

– It is difficult to turn metrics on and off as we have to manually add to code in the try>/finally block to each  and every method or constructor we want to benchmark.

– This profiling code does not belong sprinkled throughout our application code. It makes the code bloated and harder to read as we have enclose the timings within a try/finally block.

– If we want to expand this functionality to include a method or failure count or even to register these statistics to a more sophisticated reporting mechanism that we have modify a lot different files.

– Aspect oriented programming which gives we want to way encapsulate this type of behaviour functionality. It allows us to add behaviour such metrics”around” our code.

Dominant Frameworks in AOP:

AOP is having programming methods and even frameworks on which modularisation of code is supported and implemented. There are three dominant frameworks in AOP.

https://media.geeksforgeeks.org/wp-content/uploads/20190313105735/dominant-frameworks-in-AOP.jpg
  • AspectJ: It is an extension for java programming which is created at PARC research center. It uses java like syntax and including IDE integrations for displaying crosscutting structure. It has  compiler of its own and weaver, by using it enables the use of full AspectJ language.
  • JBoss: It is an open source Java application server developed by JBoss used for java development.
  • Spring: This has XML  based configuration for having AOP, and can be annotations which are said by using a library supplied by AspectJ for parsing and matching.

Common terminologies in AOP:

  1. Aspect- The class which implements the JEE application cross cutting concerns which is known as the aspect. It can be normal class configured through XML configuration or through regular classes annotated with @ Aspect.
  2. Weaving- The process of linking Aspects with an Advised object. It can be done at load time, compile time or at runtime time. Spring AOP does weaving at runtime.
  3. Advice-Here the job which is meant to be done by Aspect or it can be defined as the action taken by Aspect at a particular point. There are five types of advices are
  • Before
  • After
  • Around
  • AfterThrowing 
  • AfterReturning

AOP will allow  for the creation of relationships between the different classes. These relationships that are arbitrary, but can be used to encapsulate the housekeeping code needed to create compatibility between two classes. Here the messaging example will include the messaging information needed for updating the state of the system. As the encapsulation the class like headphone in the ongoing example but also in other systems like personal music player an interface to an airplane sound system or a particular type. It requires an creation of new languages construct that can associate an aspect to a specific class to this end has many languages.

Questions

1. What is Aspect oriented programming?

2. Explain the working principle of AOP?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class