When do you need to group actions around you into one and the same aspect, and when do you need to create different aspects?

I am currently studying Aspect-Oriented Programming , and while I was trying to practice a little, I realized that I was developing different methods in one aspect, and I began to wonder if this is the best way to do this, or if I have to create different aspects for different methods. I will try to explain more what I did: I had 2 classes:

  • Calculator The class had several methods, such as add, multiply, negate, reset, etc.
  • class AOPmain .

I created another class ( ReturnMessage ) as an aspect , and in this class I created 2 methods - as you can see in the code below) (One is used in half of the methods found in the Calculator class, and the second for some other methods in the same classroom - Calculator ). My question is: is it correct to write several methods in one aspect to interact with other methods, or would it be better to create different aspects for it? Here is the code for my aspect :

@Aspect public class ReturnMessage { //the joinPoint is used to get the method names and args. @Before("execution(public void *(double))") public void returningMessage( JoinPoint jp) { String method = jp.getSignature().getName(); double value = (Double) jp.getArgs()[0]; System.out.println("Going to "+method+" "+value); } @Before("@annotation(lala)") public void returnMsg2(MyAnnotation lala) { //<-- should I create another aspect to put this method? System.out.println(lala.msg()); } } 

I believe that this will be interesting not only for me, but for all stackoverflow users who start programming in aop, so I hope you guys will be calm.

+4
source share
3 answers

I would create an Aspect for every cross-connect issue that I want to track. It is normal to have multiple pointcut / method in your aspect.

You may have Aspect in charge of logging, one for metrics, etc.

I found this link very useful when I was working with AoP to measure perf. existing application.

It talks about the aspect of the existing code base, but it can be applied to any case.

Applying AspectJ to an Existing Codebase

+4
source

Even if they are not ordinary objects, Aspects are software components, they must respect the principles of good design - including high cohesion . I'm not an AspectJ expert, but I would say that high cohesion is the most important thing to consider when deciding whether you want to add a method to your aspect.

Aspect-oriented programming already has the potential to make execution paths a little more obscure, so I don't think that dividing each method into its own aspect would be a good idea. This would lead to many "dude where my code" games for future developers working on the same system. I think it would be better to combine the methods together in Aspect so that they are easier to find.

+1
source

An aspect also a class , and as others have answered, it should consist of related functions. To rephrase Cygnusx1, you do not want one aspect to have tips for logging, as well as creating metrics.

Further organization may be required based on points aimed at them. For example, note that the Logger aspect generates several different logs, for example. concise and verbose.

It would be wise to create this aspect in the com.company.project.logging package. But do you want him to refer to all the places in the code that need to be reported? Probably no.

What I would do is create an abstract aspect and expand it, containing concrete recommendations and abstract named points. Then I would expand on this with specific aspects in the various packages com.company.project.dataaccess , com.company.project.purchase , etc. that implement these abstract pointcuts.

So, what I did, an aspect is revealed that registers and allows individual module owners to determine when they should be launched.

+1
source

All Articles