Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AOP... Should I unlearn OOP?

Tags:

oop

paradigms

aop

I have skimmed the online documentation, read the wiki entry, the posts and the blogs, but I'm still puzzled.

  • What is, in a nutshell, Aspect Oriented Programming ?
  • Is it simply better then Object Oriented Programming ? Should I unlearn OOP ?
  • If not, how do I know when to use one or the other ? What are the main differences between the two ?
  • Can I refact one into the other ?

I have always been an OO man and I want to know if I need to commit treason.

Seriously, I'm starting a new project soon and I want to make the right choices at the beginning.

like image 624
Philippe Carriere Avatar asked Aug 24 '09 23:08

Philippe Carriere


1 Answers

What is, in a nutshell, Aspect Oriented Programming ?

In a nutshell, AOP is the ability to inject actions into the typical flow of another program, unobtrusively. It lets you capture class instantiations, method calls, assignments, etc.

Is it simply better then Object Oriented Programming ? Should I unlearn OOP ?

No, and no. It works together with any programming environment that supports it. (See above).

If not, how do I know when to use one or the other ? What are the main differences between the two ?

You use AOP, generally, when you want to implement some sort of actions on hundreds of classes, without manipulating the classes themselves. Typical examples are security (authorisation of the right to call the given method/class) or logging. In my experience, though, I don't use it for this. (I don't use it at all, honestly).

As above, the main difference doesn't really exist, as they aren't comparable. But, say you want to implement logging "normally", you just call the logger at appropriate points:

log.write("hello");

But with AOP, you may create an 'aspect' that attaches to each method call, and log 'Method b called'. The point is that in AOP you approach is more 'shotgun': you attach to everything, or just a small subset. Manually adding logging is generally better.

Can I refact one into the other ?

Not really relevant, see other answers. Again with security, you could swap to an AOP model from a typical OOP model this.IsAllowed() to something like if(callingMethod.HasAttribute(foo)){ allowed = true; }

Hope those answers are useful. Let me know if you want me to expand further.

like image 84
Noon Silk Avatar answered Feb 15 '23 16:02

Noon Silk