What is Aspect-Oriented Programming?

What is Aspect-Oriented Programming? I heard about it in a conversation between colleagues those days. No one knew how to explain me well and more, they said it was something bad.

Author: Caique C., 2014-06-10

2 answers

In summary, Aspect-Oriented Programming or Aspect-Oriented Progrmming (AOP) is a programming model that enables proper separation of responsibilities, considering features that are essential to a group of objects, but are not directly responsible for them.

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

Examples of application of AOP are: log 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 the example so as not to hurt principles and good practices of object orientation.

See 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 system methods.
  • 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? Go explicit:

  • dependencies : a framework injects the dependencies, so you don't need "dirty" signatures or constructors, that is, with parameters that are foreign to logic.
  • Security : frameworks can intercept methods, so I assume your framework will intercept the call to all methods that have the @UsuarioDeveEstarLogado annotation and execute a logic you write in one place.
  • transaction : the framework creates automatically the transaction when it finds the annotation @Transactional. It also handles commit or rollback depending on whether or not the method threw an exception.
  • Logging : the AOP framework can automatically intercept and log all method calls and exceptions that occur.
 14
Author: utluiz, 2014-06-11 21:14:55

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

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

In computer science, aspect-oriented programming or poa, is a paradigm of computer programming 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 the behavior of the object. This code is all that is used to implement secondary functionality and is spread over the whole application (crosscutting concern). POA allows this code to be encapsulated and modularized.

Reference: http://pt.wikipedia.org/wiki/Programa%C3%A7%C3%A3o_orientada_a_aspecto

It is known that in software development there are properties that do not fit into components of functional decomposition, such as: treatment of exceptions, restrictions of real time, distribution and concurrency control. They usually are scattered in various system components affecting the performance or semantics of the application.

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

The programming oriented to aspects is an approach that allows the separation of these orthogonal properties of functional components in a natural and concise way, using abstraction and compositional mechanisms for Code production executable.

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

When something similar to the audit log occurs, i.e. an interest traversing the application, inferring many modules, or more directly, this interest "cut" the application, we say if it is a choppy interest. The goal of Aspect-Oriented Development is to encapsulate these choppy interests into physically separate modules from the rest of the code. The modules that house these interests are called aspects. If we think in abstract terms, aspect orientation introduces a third dimension of decomposition. Remembering what we saw, 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 in a distinct module, we will have the orientation to aspect.

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

 6
Author: LeoFelipe, 2014-06-10 19:23:04