现在的位置: 首页 > 综合 > 正文

What Is AOP?

2011年11月24日 ⁄ 综合 ⁄ 共 4590字 ⁄ 字号 评论关闭

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 with. 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. Many 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 could 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 above can 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 includes 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.

抱歉!评论已关闭.