الأحد، 10 يناير 2010

Aspect Oriented Programming

In computing, aspect-oriented programming (AOP) is a programming paradigm in which secondary or supporting functions are isolated from the main program's business logic. It aims to increase modularity by allowing the separation of cross-cutting concerns, forming a basis for aspect-oriented software development.

AOP addresses the problem of cross-cutting concerns, which would be any kind of code that is repeated in different methods and can't normally be completely refactored into its own module, like with logging or verification. So, with AOP you can leave that stuff out of the main code and define it vertically like so:

function mainProgram()
{ var x = foo();
doSomethingWith(x);
return x;
}

aspect logging
{ before (mainProgram is called):
{ log.Write("entering mainProgram");
}

after (mainProgram is called):
{ log.Write( "exiting mainProgram with return value of "
+ mainProgram.returnValue);
}
}

aspect verification
{ before (doSomethingWith is called):
{ if (doSomethingWith.arguments[0] == null)
{ throw NullArgumentException();
}

if (!doSomethingWith.caller.isAuthenticated)
{ throw Securityexception();
}
}
}


And then an aspect-weaver is used to compile the code into this:



function mainProgram()
{ log.Write("entering mainProgram");

var x = foo();

if (x == null) throw NullArgumentException();
if (!mainProgramIsAuthenticated()) throw Securityexception();
doSomethingWith(x);

log.Write("exiting mainProgram with return value of "+ x);
return x;
}


There are two different ways AOP programs can affect other programs, depending on the underlying languages and environments:
(1) a combined program is produced, valid in the original language and indistinguishable from an ordinary program to the ultimate interpreter
(2) the ultimate interpreter or environment is updated to understand and implement AOP features.

The following are some standard terminology used in AOP:

  • Cross-cutting concerns: Even though most classes in an OO model will perform a single, specific function, they often share common, secondary requirements with other classes.
  • Advice: This is the additional code that you want to apply to your existing model.
  • Pointcut: This is the term given to the point of execution in the application at which cross-cutting concern needs to be applied.
  • Aspect: The combination of the pointcut and the advice is termed an aspect.
AOP has been implemented (in either the language itself or as external library) for the following programming languages & tools:
  1. Java AspectJ
  2. .Net framework (C# & VB.Net)
  3. C/C++
  4. PostSharp

هناك تعليق واحد:

MetaProgrammer يقول...

انا تبرعت بكومنت