preface
AOP’s full name is Aspect Oriented Programming, meaning: section-oriented Programming.
Why does this article exist? Because I read the design of Pipeline in Laravel and found that Pipeline is an implementation based on AOP ideas.
Speaking of AOP, we have to speak of OOP, what is the relationship between them, what is the difference?
The difference between AOP and OOP
OOP, as we all know, is the whole process of Object Oriented Programming.
First of all, AOP and OOP are not antithetical to each other. AOP can be thought of as complementing OOP’s strengths and weaknesses, and the two work best together.
OOP abstracts and encapsulates business entities and their properties and behaviors, for example, user modules, order modules, and so on.
AOP is for the business aspect of the extraction, it is facing a certain step or stage in the process, to achieve the isolation effect of low coupling between the parts of the logical process, such as: logging, permission verification, etc.
For example, it is easy to understand what happens if you use OOP purely and need permission validation and logging in the logging module, the order module? Do you want to put permission validation, logging code in front of every method? So what if you need to log before and after every method?
If you use AOP, you can do these repetitive operations with proxies, and you can reduce the coupling between the parts by not putting permission validation, logging code in front of each method.
What can AOP do
In addition to the above mentioned permission validation, logging, AOP can also do data encryption and decryption, request response data specification…
As long as it is irrelevant to the specific business and the business is concerned, AOP can be used to separate these concerns and maintain them in a unified manner, improving code reuse.
Does the business focus above sound familiar… In fact, we commonly used routing middleware is a kind of realization based on AOP thought.
An implementation of AOP
Example: Routing middleware in Laravel.
/**
* Send the given request through the middleware / router.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendRequestThroughRouter($request)
{
$this->app->instance('request', $request);
Facade::clearResolvedInstance('request');
$this->bootstrap();
return (new Pipeline($this->app))
->send($request)
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
->then($this->dispatchToRouter());
}
Copy the code
From the code above, you can see that the middleware that needs to be executed is configured in the through() method, followed by the then method.
The above code is used in the routing middleware, but it can also be used elsewhere, such as in the Controller:
$pipes = [Pipeline::class, // Pipeline::class, // pipeline ::class, // pipeline ::class]; return app(Pipeline::class) ->send($request->all()) ->through($pipes) ->then(function ($content) { return $content; });Copy the code
This is just one implementation in Laravel, but there are similar implementations in other PHP frameworks, such as Yii, ThinkPHP, etc.
AOP is just an idea, but there are other implementations, such as Golang and Java.
Recommended reading
- Programming is an idea, not code
- How to do business system research and development seriously and responsibly?