views:

53

answers:

4

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?

+1  A: 

What language are you talking about here?

And no, this is not how AOP is generally done, and probably not possible in languages with such frameworks (C#, .NET).

What you're talking about here is a pre-build phase, i.e. you have code that doesn't compile now, and you need to make it compile before sending it to the actual compiler ... AKA as code generation. code generation is a fine thing to do, and there are frameworks for it in various languages (so again, tell us what language).

But is it AOP? No.

AOP is about injecting functionality into already-existing methods. I.e. "hooking" functions/actions, etc.

Noon Silk
I'm not talking about a specific language - just asking a question about mixing interfaced based composition style programming with aspect oriented programming. Thanks for the answer
sylvanaar
sylvanaar: AOP implementations are language-specific, so your question can't be fully answered unless you provide one. Regardless, though, my comment still stands. What you're more interested in here is Code Generation (at various levels, perhaps).
Noon Silk
Alright, i was under the impression that AOP was a style. For the same of the question, lets say Java for a statically typed language, and Lua or Python for a dynamically typed language.I do agree - a straightforward way to accomplish what i am talking about is through code generation.
sylvanaar
AOP is not really a style, it's a way of hooking into existing functions/methods etc in a given language. The implementation and points you can hook onto/at are specific to the language and AOP framework you choose.
Noon Silk
+1  A: 

AOP is typically about applying a cross-cutting concern across a rance of procedures without affecting the implementation of those procedures. Audit Logging is a good example. You might define an aspect that says "all operations affecting customer accounts should be logged". You can implement this procedurally in all operations that work with the account, or you may wish to pull this code out to the side.

This is ususally implemented by inserting behavior around existing code. In OO, such aspects can be applied by means of the proxy pattern. Most C# and Java AOP frameworks will generate a proxy class at runtime based on metadata in the target class, and cause some code to run before and after specified methods.

A lot of IoC containers provide functionality for attaching these aspects, and the concepts of aspects, method interception, and dependency injection are harder to distinguish than they once were.

BnWasteland
+1  A: 

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.

Pedro Ghilardi
A: 

No, that's not really what AOP is for. AOP is not for implementing interfaces at runtime but for composing systems: business logic may be defined in one place for example and logging logic in another and an AOP framework will then compose the two into a bigger system. AOP is designed to cover what it refers to as cross-cutting concerns, that is functionality that is required by many objects within a system but which is not core to the concerns of those objects.

AOP works by intercepting calls to methods on objects and performing some action in addition to, or instead of, the actions performed by the intercepted method. The interception points are known as pointcuts and the intercepted method is the advised method, with the code being advised on the intercepted method being known as the advice.

I am only familiar with AOP via Spring.Net's AOP Framework which allows you to specify and apply pointcuts and advices both via configuration files and programmatically. Spring.Net AOP has four types of advice: before, after, around and throws which are invoked on interception of an advised method before the advised method is invoked, after it is invoked, both before and after its invocation and when an exception is thrown respectively. Whether applied via configuration or programmatically, the advised method has no knowledge of Spring.Net AOP or even that it has been advised. The advised method must however have some kind of implementation to intercept so your example would not work.

The Spring.Net documentation is very readable and is very good at explaining AOP in general and Spring.Net's implementation of AOP in particular and contains many examples. It is well worth a look even if just to understand AOP a bit better.

Alfamale