Copyright

Aspect Oriented Programming: Definition & Concepts

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Nothing remains constant in the world of programming. Object-oriented programming may still hold court, but a new concept is in town: Aspect-Oriented Programming (AOP). We will define the term and provide some key concepts for this methodology.

Meet AOP

Aspect-Oriented Programming (AOP) is a new programming concept that was developed due to some shortcomings of object-oriented programming (OOP). Like OOP, AOP defines methods for programming and tools; however these look quite different. The main focus of the paradigm is an aspect, or feature that can be found across methods and classes. Where OOP deals with object, AOP concerns itself with aspects.

Aspects

Instead of a concrete object, such as Engine, Aspect-Oriented Programming (AOP) is concerned with aspects. These might be a little hard to pin down, since they can cross over classes and methods. When discussing AOP, you will hear programmers refer to crosscutting - an action or behavior that cuts across several areas of your program. Think of a large city with streets laid out in a grid. In some cities, however, a street will cut diagonally across the city center.

An example of an aspect is a log or monitor. We may need to know who updated what, and when. Tagging a monitor to every class in your code is a lot of overhead. It's not something an Engine class should be doing. It should be worried about starting, stopping, running. In fact, you may have an entire object model of a car, with Engine, Exhaust, and other classes. The monitoring/logs will crosscut all objects and methods.

It would be very inefficient if you had to add logging to every class in your program, especially a program that does banking or financial transactions. The aspect cuts across dozens of classes! And if you had a typo or an error, it could be a nightmare finding it.

The following figure displays the crux of our problem.

AOP crosscut

To compare to a diagonal street: We would have to install traffic lights and signs at each intersection. There must be a better way! The Aspect-Oriented approach allows us to remove that diagonal street and WRAP the behavior around our code. We would then have the following example:

AOP wraps around code

AOP allows us to wrap the behavior around the code and avoid the diagonal street.

AOP Concepts in Action

We won't get into the nitty-gritty details of an AOP solution. However, you are probably wondering how this actually works.

AOP allows you to create your method as its own class. In AOP we would call this class an interceptor. This class will carry out the actual logging. The benefit to this stand-alone class is that you can add/remove logging functionality at any point. You don't have to replace every No Parking sign with a Free Parking sign on the diagonal street! You simply update the interceptor code.

Having the interceptor is great, but in order to actually use it, we need to specify WHEN to run the aspect code. The places where you call the code are called pointcuts. Think of an entry point or flag that triggers the execution of the interceptor. This could be a call to a method or constructor, even at a point when an exception is created.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support