What is aspect-oriented programming?

Asked

Viewed 5,822 times

17

What is aspect-oriented programming? I heard about it in a conversation between colleagues these days. Nobody explained me well and more, said it was something bad.

  • 10

    disseram que era algo ruim NEVER Listen to anyone who says that kind of thing without giving an explanation. In the middle ages it was said that tomatoes were poisonous and nobody ate them, but no explanation was given. Today we have ketchup. Think about it ;)

  • 2

    @Renan must be because of the Tomatin that’s near the stem and is toxic. Or maybe someone died at the Italian festival of the same name... /s :) Vegetables aside, I agree with his comment and extend it to such "good practices", which usually sound like a synonym for "I’m not sure what I’m doing, but they said it’s good". ;) As to the question, +1.

  • 1

    @But I think ketchup bad...

2 answers

14


In summary, the Programming Oriented to Aspects or Aspect-Oriented Progrmming (AOP) is a programming model that enables the proper separation of responsibilities, considering functionalities that are essential for a group of objects, but are not their direct responsibility.

AOP is said to be orthogonal because it usually involves concepts that are independent of the layer and have no direct relation to the functional requirements of a system.

AOP application examples are: logging generation, access control and exceptional treatments, transactions.

For example, the method below (in Java) contains fictitious business logic and various treatments needed in any enterprise system: transaction, security and logging. I tried to make an example so as not to hurt principles and good practices of Object Orientation.

See the method:

//Inversão de Controle: dependências são recebidas por parâmetros
void processarOperacao(TransactionManager transacao, LogManager log, SecurityManager seguranca, OperacaoDAO dao, Operacao operacao) {
    try {

        //segurança
        if (seguranca.usuarioEstaLogado()) {
            log.info("Falha de segurança"); //log
            throw new AcessoNegadoException();
        }

        //transação
        transacao.begin();
        processamentoComplexoOperacao(operacao);
        dao.salvarOperacao(operacao);
        transacao.commit();
        log.info("Sucesso"); //log

    } catch (ErroNegocioException e) {
        //tratamento excepcional
        log.error("Falha", e); //log
        transacao.rollback(); 
    }
}

The main problems with the above code are:

  • It will repeat itself in all methods of the system.
  • 99% of the code deals with orthogonal things, that is, business logic is lost in the midst of various technical issues.

With AOP we can let the developer focus on logic and delegate these other details to a framework or container with AOP support.

Let’s now see an example using AOP:

//dependências
@Inject OperacaoDao;

@UsuarioDeveEstarLogado //segurança
@Transactional //transação
void processarOperacao(Operacao operacao) {

    processamentoComplexoOperacao(operacao);
    dao.salvarOperacao(operacao);

}

Did you see the difference? I’ll make it clear:

  • Dependencies: a framework injects the dependencies, so you don’t need "dirty" signatures or constructors, that is, with parameters foreign to logic.
  • Security: frameworks can intercept methods, so I assume your framework will intercept the call to all methods that have the annotation @UsuarioDeveEstarLogado and execute a logic you write in one place.
  • Transaction: the framework automatically creates the transaction when it finds the annotation @Transactional. He also control the commit or rollback depending on whether or not the method launched an exception.
  • Logging: the AOP framework can automatically intercept and log all calls to methods and exceptions that occur.
  • So in the end it continues to be object-oriented, but with a larger asbtraction towards logic, leaving separate code not pertinent to it?

  • 1

    @Caiquec. It would be possible to use AOP with procedural programming, intercepting calls to functions and subroutines and automatically adding transaction, security, etc. The language only needs to support interception techniques or allow access to methods by reference, for example. But I don’t know AOP implementations other than OO.

  • So the aspect-oriented programming at the end would not be a paradigm? Would it be more of a paradigm support? Aspect support? rs

  • 2

    @Caiquec. Just because AOP works with other paradigms doesn’t mean it can’t be a paradigm. Python is a language that can be used in a structured way only with functions or oriented to objects. One thing does not automatically exclude the other except in people’s minds.

  • I get it, thank you :)

6

About being bad or not, it doesn’t exist, what exists is whether it is suitable or not to a certain situation and will depend a lot on the developer and what is being developed. Particularly, the POO always solved our problems perfectly, so we never had the need to develop something in a new paradigm. And as an observation we can use POO together with POA.

It follows some texts and their references that I have here in my history.

In computer science, aspect-oriented programming or POA, it is a computer programming paradigm that allows software developers to separate and organize code according to its importance to the application (Separation of Concerns). The entire program written in the object-oriented paradigm has code that is unrelated to the implementation of object behavior. This code is all that used to implement secondary functionalities and that is spread throughout the application (crosscutting Concern). The POA allows this code to be encapsulated and modularized.

Reference: http://en.wikipedia.org/wiki/Aspect-oriented_programming

It is known that in software development there are properties that do not fit in functional decomposition components, such as exception handling, restriction of real time, distribution and competition control. They are usually spread in system components affecting performance or application semantics.

Although they can be viewed and analyzed relatively separately, their implementation using object-oriented or structured languages becomes confusing and your code is spread through the application code, making it difficult to separate the basic system functionality of these properties.

Aspect-oriented programming is an approach that allows the separation of these orthogonal properties of the functional components in a natural and concise way, using mechanisms of abstraction and composition for the production of code executable.

Reference: http://www.ic.unicamp.br/~rocha/college/src/aop.pdf

When something similar to the audit log occurs, that is, an interest traversing the application, inferring many modules, or more directly, this interest "cut" the application, we say it is a cross-cutting interest. The goal of aspect-oriented development is to encapsulate these interlocking interests in modules physically separate from the rest of the code. The modules that harbor these interests are called aspects. If we think in abstract terms, aspect orientation introduces a third dimension of decomposition. Remembering what we saw, the OO decomposes the system into objects (data) and methods (functions). In turn, objects and methods can still be decomposed according to a common interest. By grouping each interest into a distinct module, we will have the aspect orientation.

Reference: http://www.devmedia.com.br/introducao-a-orientacao-a-aspecto/27759

Browser other questions tagged

You are not signed in. Login or sign up in order to post.