Home >Backend Development >PHP Tutorial >What is adapter mode?

What is adapter mode?

藏色散人
藏色散人forward
2019-04-16 09:51:224570browse

Adapter Pattern

The Adapter pattern is also called the adapter pattern. It is one of the structural patterns. Existing classes can be changed through the Adapter pattern ( or external class) interface form.

Adapter pattern application scenarios

In the process of large-scale system development, we often encounter situations such as the following:

We need to implement a certain These functions already have one or more external components that are not yet mature. If we re-develop these functions ourselves, it will take a lot of time; so in many cases we will choose to use external components temporarily and then consider replacing them at any time later. But this will bring about a problem. With the replacement of the external component library, it may be necessary to make extensive modifications to the source code that references the external component, so it is very likely to introduce new problems and so on. How to minimize the modification area? The Adapter pattern was proposed for this similar need. The Adapter pattern transparently calls external components by defining a new interface (which abstracts the functions to be implemented) and an Adapter class that implements the interface. In this way, when replacing external components, you only need to modify a few Adapter classes at most, and other source codes will not be affected.

php example

Suppose we have an article class that has completed the list of articles and detailed information display:

<?php
class article{
//文章列表获取方法
public function getLIst(){
echo &#39;获取文章列表&#39;;
}
//根据文章id获取文章的标题和内容
public function getInfo($id){
echo &#39;根据文章id获取文章的标题和内容&#39;;
}
}
$art = new article();
$art->getInfo(1);

Due to the needs of the project, Such a function is now needed. When obtaining article details, you also need to obtain the creation time of the article and update the number of times the article has been read.

If we do not use the adapter mode, our first thought is to modify the source code of the article class to add such a function. The reason why I think this is because the example code above is very simple. What if it is thousands of lines? What if the getInfo() code is very complex?

Use the adapter pattern to solve all this!

class articleAdapter{
public $_artObj;
public function __construct($artObj){
$this->_artObj = $artObj;
}
public function getInfo($id){
$this->_artObj->getInfo($id);
}
public function getInfoAndUpdate($id){
//利用$this->_artObj查询符合要求的文章数据并更新浏览次数
echo &#39;$this->_artObj查询符合要求的文章数据并更新浏览次数&#39;;
}
}
$art = new articleAdapter(new article());
$art->getInfo(12);
$art->getInfoAndUpdate(12);

Why not use inheritance?

Object adapter: It is not processed through inheritance, but through object combination. As long as we have studied OO design principles, we all know that combination is the recommended way compared to inheritance. .

Class adapter: implemented through inheritance, encapsulating the methods of the old system. In the process of converting object adapters between adapters, class adapters can undoubtedly be completed, but the dependencies will increase, and with the flexibility of adaptation requirements, the expansion may become difficult to control through inheritance.

Generally speaking, class adapters are less flexible, while object adapters are more flexible. This is the recommended method. It can be done through dependency injection or configuration. The class adapter needs to inherit from the class of the old system to be adapted, which is undoubtedly not a good idea.

The above is the detailed content of What is adapter mode?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:hcoder.net. If there is any infringement, please contact admin@php.cn delete