foreword
AOP
full name Aspect Oriented Programming , meaning: Aspect Oriented Programming.
Why is there this article? Because watching Laravel in Pipeline design , we found Pipeline
is based AOP
an idea implementation.
Speaking of AOP
, we have to talk about OOP
, what is their relationship and what is the difference?
The difference between AOP and OOP
OOP
We all know that the whole process is Object Oriented Programming , which means: object-oriented programming.
First of all, we need to know that AOP
and OOP
are not in opposition to each other. We can regard AOP
OOP
. This is the best way to make up for the shortcomings of the other. The combination of the two is the best.
OOP
is for business entity and its attributes and behavior abstract encapsulation , for example: user modules, order modules, etc., this is not difficult to understand.
AOP
is extracted for the business section, it faced certain process step or phase , in order to achieve low coupling between portions of the logic processing isolation , for example: log Records, authorization verification, etc.
For example, it is easy to understand. If you simply use OOP
, you need to perform authorization verification and logging in the log module and order module. Do you want to add code for permission verification and logging before each method? So what if you need to log before and after every method?
At this time, if you use AOP
, you can use the proxy to complete these repeated operations, you can not add code for authorization verification and logging before each method, and reduce the coupling between each part.
What can AOP do
In addition to the permission verification and logging mentioned above, AOP
can also do data encryption and decryption, request and response data specification...
As long as it is not related to the specific business, and at the same time the business is concerned, then AOP
can be used to extract these concerns and maintain them uniformly to improve the reusability of the code.
above business focus sound familiar... In fact, our commonly used 161dab939ba0ec routing middleware is an implementation based on the
AOP
An implementation of AOP
Example: Laravel
routing middleware in .
/**
* 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());
}
By the above-described code can be found in the middleware necessary configuration through()
process performed after execution then
method.
The above code is used in the routing middleware, of course, it can also be used in other places, such as
controller
, it can be written like this:
// 示例代码
$pipes = [
LoggingPipeline::class, // 日志记录
PermitPipeline::class, // 权限验证
];
return app(Pipeline::class)
->send($request->all())
->through($pipes)
->then(function ($content) {
return $content;
});
The above only Laravel
one implementation, of course, in PHP
other frame also has a similar implementation, for example: Yii
, ThinkPHP
like.
AOP
is just an idea, of course, there are other language implementations, such as: Golang
and Java
and so on.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。