AOP (aspect-oriented programming) – is a paradigm aimed at increasing the modularity of different parts of an application by separating end-to-end tasks. To do this, additional behavior is added to the existing code, without changing the original code.
In other words, it’s as if we’re superimposing additional functionality on top of methods and classes, without amending the code being modified.
Why do we need it? Sooner or later we come to the point that usual object-oriented approach can’t always solve certain tasks efficiently. At such a moment AOP comes to our aid and gives us additional tools to build the application. Additional benefits are an increase in flexibility during development, due to which there are more variants of solving this or that task.
Benefits of Aspect Oriented Programming
Aspect-oriented programming is intended for solving end-to-end tasks, which can be any code repeated many times using different methods that cannot be completely structured in a separate module.
Accordingly, with AOP we can leave this out of the core code and define it vertically.
An example would be the usage of a security policy in an application.
As a rule, security runs through many elements of an application. All the more so, an application’s security policy should be applied equally to all existing and new parts of the application. At the same time the security policy can evolve on its own.
This is where the use of AOP comes in handy.
Another example is the logging approach.
Using the AOP approach to logging has several advantages over the manual insertion of logging:
- The code for logging is easy to implement and remove: all you have to do is add or remove a couple of configurations of some aspect.
- All source code for logging is stored in one place and you don’t need to find all the usage places manually.
- The code intended for logging can be added anywhere, whether it’s methods and classes already written or new functionality. This reduces the number of developer errors.
Also, when removing an aspect from a design configuration, you can be absolutely sure that all tracing code has been removed and nothing has been missed. - Aspects are separate code that can be reused and improved many times.