In the previous article " A Brief Talk on the Decorator Mode in PHP" we introduced the decorator mode in PHP. This article will take you to understand the adapter mode in PHP.
There has always been a classic example of this model, and that is the socket! That's right, when we buy electrical appliances from abroad, or travel abroad on business, we often need a power adapter, because our country's voltage standard is 220 volts, while other countries have 110 volt standards. And this power adapter is a symbol of adapter mode. When the object does not meet the requirements, add an adapter to it! !
GoF definition: Convert the interface of a class into another interface that the customer wants. The Adapter pattern enables classes that originally could not work together due to incompatible interfaces to work together
GoF class diagram:
Inheritance
Combined
Code implementation
interface Target{ function Request() : void; }
Define an interface contract, It can also be a normal class with implementation methods (we will use classes in the following examples)
class Adapter implements Target{ private $adaptee; function __constuct($adaptee){ $this->adaptee = $adaptee; } function Request() : void { $this->adaptee->SpecificRequest(); } }
The adapter implements this interface contract so that the Request() method can be implemented, but please note that what we actually call It is a method in the Adaptee class
class Adaptee { function SpecificRequest() : void{ echo "I'm China Standard!"; } }
Let's talk about my mobile phone factory again. This time our business has really grown! It has been sold to Thailand, Singapore, and Indonesia. Anyway, we can be found wherever there is curry. It is said that we produced a curry color. The change of shell is not entirely due to the influence of Noah, but after long-term research, we found that different colors will sell better in different places. Therefore, Foxconn installed a spraying adapter (adapter) for us on the original mobile phone case production line (Target). When we need cases of other colors, we only need this adapter to change different paints (adaptee) , directly install this sprayer, and a new color mobile phone is born. When expanding our business to another country, we can just change the paint. If it takes too long, we will also replace the nozzle (remember the continuous supply of the printer)
Full code :Adapter pattern
https://github.com/zhangyue0503/designpatterns-php/blob/master/05.adapter/source/adapter.php
Continue to send text messages and see when I can compile it~~~
Everyone often uses the SDK provided by these platforms when connecting to information and payment interfaces. Especially with Composer, it is more convenient to install the SDK. However, there is another serious problem. Although the SDKs made by these people have similar functions, their names are very different! ! Our system has always used Alibaba Cloud's services, but this time we need to add the information functions of Jiguang and Baidu Cloud, first as a backup, and secondly, to use different interfaces according to different services to achieve security or economical purposes. Is there any way? Unify their external interfaces so that when we use their SDK, it can be very convenient and the same as the Alibaba Cloud interface that everyone is already used to? Of course, just give them each an adapter. When instantiating, you can just set up an external factory to return different adapters. As long as the implementation method in the adapter is the same as Alibaba Cloud, it will be OK!
SMS sending class diagram
Complete source code: SMS sending adapter method
https:/ /github.com/zhangyue0503/designpatterns-php/blob/master/05.adapter/source/adapter-message.php
<?php class Message{ public function send(){ echo "阿里云发送短信!" . PHP_EOL; } public function push(){ echo "阿里云发送推送!" . PHP_EOL; } } class JiguangSDKAdapter extends Message{ private $message; public function __construct($message){ $this->message = $message; } public function send(){ $this->message->send_out_msg(); } public function push(){ $this->message->push_msg(); } } class JiguangMessage{ public function send_out_msg(){ echo "极光发送短信!" . PHP_EOL; } public function push_msg(){ echo "极光发送推送!" . PHP_EOL; } } class BaiduYunSDKAdapter extends Message{ private $message; public function __construct($message){ $this->message = $message; } public function send(){ $this->message->transmission_msg(); } public function push(){ $this->message->transmission_push(); } } class BaiduYunMessage{ public function transmission_msg(){ echo "百度云发送短信!" . PHP_EOL; } public function transmission_push(){ echo "百度云发送推送!" . PHP_EOL; } } $jiguangMessage = new JiguangMessage(); $baiduYunMessage = new BaiduYunMessage(); $message = new Message(); // 原来的老系统发短信,使用阿里云 $message->send(); $message->push(); // 部分模块用极光发吧 $jgAdatper = new JiguangSDKAdapter($jiguangMessage); $jgAdatper->send(); $jgAdatper->push(); // 部分模块用百度云发吧 $bdAatper = new BaiduYunSDKAdapter($baiduYunMessage); $bdAatper->send(); $bdAatper->push();
Description:
Recommended learning: "PHP Video Tutorial 》
The above is the detailed content of Learn about the adapter pattern in PHP in one article. For more information, please follow other related articles on the PHP Chinese website!