R – Is aspect oriented programming useful for providing implementations for interface based programming

aopdesign-patternsinterface

I was just curious about the subject. I have never used aspect oriented programming (intentionally), and I have only a small amount of knowledge about it.

Here's my question (using logging as the typical use-case):

If i have an existing interface based paradigm for example consider the pseudo-code

class MyClass implements Loggable {

  // Logable Implementation
  string Log() { return somelogstring;}
}

Can aspect oriented programming be used along with this in a manner like

class MyClass implements Loggable with LoggingAspect {

  // No explicit Loggable implemenation
}

Is that something that would be considered AOP? If so, ss it a valid way to use it?

Best Answer

AOP was created to implement crosscutting concerns (like Logging), concerns that "cuts" between various modules on your application. If you implement the Logging with AspectJ, for example, you will have an aspect like this:

public aspect Logging(){

    pointcut traceMethods()  : (execution(* *.*(..)) && !within(Logging);

    before(): traceMethods() {
         // Perform log here!
    }

}

This code will implement the log functionality before the execution of all classes of your application. So, it will insert the behavior of logging on some classses that you want. To point what classes would be affected by the logging you should define a pointcut (or a set of pointcus). In this example, the pointcut is the traceMethods().

Thinking about interfaces you should look at this link that explains Inter-type declarations. This declarations could be used to 'implement' something like interfaces using AspectJ.

Related Topic