What Is AOP?

Source: Internet
Author: User
Tags introductions

Return to Contents

2. What Is AOP?

Before all, pay attention that the word "AOP" here stands for "Aspect Oriented Programming" not "Agent Oriented Programming ". shortly speaking, AOP enables the modularization of crosscutting concerns within software systems. it provides a new aspect oriented design pattern over OOP. to go closer to AOP let's begin with some definitions first.

2.1 Definitions

Join Point.A Join Point is a well-defined point in the execution of a system. join points are those elements of the programming language semantics which the aspects coordinate. every AOP system defines a subset of exposed join points available for crosscutting. commonly exposed join points include method call, object creation, method execution, getting/setting a field/property, and exception handler execution. but they heavily depend on the underlying programming language and AO language. also, an AOP system limits exposed join points to promote robust aspects. join points can be picked up by an AOP program by using pointcuts to match on them. and since join points are dynamic, it may be possible to expose runtime information such as the caller or callee of a method from a join point to a matching pointcut.

Pointcut.A pointcut is a program element that selects join points and extract context. A pointcut selects join points based on there characteristics such as class names method names, argument types, and exception specifications, and can extract runtime information, such as the target object description and the method arguments, to a piece of advice.

Advice.Advice is extra code to be executed upon reaching selected join points. typically, advice comes in three flavors: before, after and around. A before advice executes before the join point's execution, whereas an after advice executes after the join point's execution. an after advice may allow discrimination based on whether the join point executed successfully or failed with an exception. and a round advice surrounds the join point execution and takes control. it may execute the join point with the original context or change the context (such as parameters ), and it may not execute the join point at all but execute extra codes defined in the advice instead or even execute it multiple times mixed with extra codes.

Introduction.Introduction, also called inner-type declaration, enables modifications of types in the target system, such as introducing new methods and fields to a type, declaring a new parent type for existing type, and modifying the exception specification of a method.

Aspect.Aspects are one kind of concern in software development. it is the unit of crosscutting modularity, and it holds any of the crosscutting elements. an aspect may be related to another aspect by inheritance relationship, or an aspect may be related to other aspects by a precedence relationship, which ensures a deterministic behavior when multiple aspects affect the some parts of the system. aspects are instantiated automatically by the AOP system. using kinds crosscutting concerns like logging, caching, security checking and exception handling may be designed as separately aspects in a software system.

2.2 Live Example

Let's focus on a code example now. Suppose below is a simple class of a software system:

Public class Dog
{
Private string _ Gender;

Public Dog ()
{
_ Gender = "Male ";
}

Public string Gender
{
Get {return _ Gender ;}
Set {_ Gender = value ;}
}

Public void Bark ()
{
//...
}
}

What can be join points? The constructor "Public Dog ()", the getter and setter method of property "Gender", and the method "Public void Bark ()".

What can be a pointcut? As an example, I just use natural English to define a sample pointcut: Pointcut1 = "The constructor public Dog () and the getter/setter method of property Gender and the method public void Bark ()". we can see this pointcut selects all the constructors, getter/setter methods of properties and member methods of this class. of course, if you want you cocould only include some of them not all.

Now let's define a sample advice in a faked language format:

Before: Pointcut1
{
Logger. write (GenerateContextDescription ());
}

This advice means a line of logging code be executed before the join points selected by Pointcut1. In this example, it also means before constructing a Dog or getting/setting the Gender property of a Dog or a Dog Bark, the logging codes defined in the advice will be executed.

Now let's define a sample introduction in a faked language format:

Public class Animal
{
//...
}

Add parent: Dog extends Animal

In these codes, we define a base class Animal and through introduction, set it as the parent class of class Dog, which means a Dog also being an Animal now.

And what can be an aspect? That may be a collection of pointcuts, advices and introductions that focus on a certain concern. As an example, all the pointcuts, advices and introductions abocan be called as a Demo Aspect.

2.3 What Is Aspect Oriented Programming?

So, Aspect Oriented Programming means extracting a software system into a core implementing and some separately aspects, and each aspect between des a collection of pointcuts, advices and introductions, focusing on a certain concern of the system. and we can develop/debug the core implementing and each aspect independently and merge all or parts of them when necessary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.