您好,欢迎访问一九零五行业门户网

详述PHP适配器模式(附代码示例)

本篇文章给大家带来了关于php的相关知识,其中主要跟大家聊一聊php适配器模式,还有代码示例,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。
php 适配器模式讲解和代码示例
适配器是一种结构型设计模式, 它能使不兼容的对象能够相互合作。
适配器可担任两个对象间的封装器, 它会接收对于一个对象的调用, 并将其转换为另一个对象可识别的格式和接口。
复杂度:******
流行度:******
使用示例: 适配器模式在 php 代码中很常见。 基于一些遗留代码的系统常常会使用该模式。 在这种情况下, 适配器让遗留代码与现代的类得以相互合作。
识别方法: 适配器可以通过以不同抽象或接口类型实例为参数的构造函数来识别。 当适配器的任何方法被调用时, 它会将参数转换为合适的格式, 然后将调用定向到其封装对象中的一个或多个方法。
真实世界示例适配器允许你使用第三方或遗留系统的类, 即使它们与你的代码不兼容。 例如, 你可以创建一系列特殊的封装器, 来让应用所发出的调用与第三方类所要求的接口与格式适配, 而无需重写应用的通知接口以使其支持每一个第三方服务 (如钉钉、 微信、 短信或其他任何服务)。
 index.php: 真实世界示例
<?phpnamespace refactoringguru\adapter\realworld;/** * the target interface represents the interface that your application's classes * already follow. */interface notification{ public function send(string $title, string $message);}/** * here's an example of the existing class that follows the target interface. * * the truth is that many real apps may not have this interface clearly defined. * if you're in that boat, your best bet would be to extend the adapter from one * of your application's existing classes. if that's awkward (for instance, * slacknotification doesn't feel like a subclass of emailnotification), then * extracting an interface should be your first step. */class emailnotification implements notification{ private $adminemail; public function __construct(string $adminemail) { $this->adminemail = $adminemail; } public function send(string $title, string $message): void { mail($this->adminemail, $title, $message); echo "sent email with title '$title' to '{$this->adminemail}' that says '$message'."; }}/** * the adaptee is some useful class, incompatible with the target interface. you * can't just go in and change the code of the class to follow the target * interface, since the code might be provided by a 3rd-party library. */class slackapi{ private $login; private $apikey; public function __construct(string $login, string $apikey) { $this->login = $login; $this->apikey = $apikey; } public function login(): void { // send authentication request to slack web service. echo "logged in to a slack account '{$this->login}'.\n"; } public function sendmessage(string $chatid, string $message): void { // send message post request to slack web service. echo "posted following message into the '$chatid' chat: '$message'.\n"; }}/** * the adapter is a class that links the target interface and the adaptee class. * in this case, it allows the application to send notifications using slack * api. */class slacknotification implements notification{ private $slack; private $chatid; public function __construct(slackapi $slack, string $chatid) { $this->slack = $slack; $this->chatid = $chatid; } /** * an adapter is not only capable of adapting interfaces, but it can also * convert incoming data to the format required by the adaptee. */ public function send(string $title, string $message): void { $slackmessage = "#" . $title . "# " . strip_tags($message); $this->slack->login(); $this->slack->sendmessage($this->chatid, $slackmessage); }}/** * the client code can work with any class that follows the target interface. */function clientcode(notification $notification){ // ... echo $notification->send("website is down!", "<strong style='color:red;font-size: 50px;'>alert!</strong> " . "our website is not responding. call admins and bring it up!"); // ...}echo "client code is designed correctly and works with email notifications:\n";$notification = new emailnotification("developers@example.com");clientcode($notification);echo "\n\n";echo "the same client code can work with other classes via adapter:\n";$slackapi = new slackapi("example.com", "xxxxxxxx");$notification = new slacknotification($slackapi, "example.com developers");clientcode($notification);
output.txt: 执行结果
client code is designed correctly and works with email notifications:sent email with title 'website is down!' to 'developers@example.com' that says '<strong style='color:red;font-size: 50px;'>alert!</strong> our website is not responding. call admins and bring it up!'.the same client code can work with other classes via adapter:logged in to a slack account 'example.com'.posted following message into the 'example.com developers' chat: '#website is down!# alert! our website is not responding. call admins and bring it up!'.
概念示例本例说明了适配器设计模式的结构并重点回答了下面的问题:
它由哪些类组成?这些类扮演了哪些角色?模式中的各个元素会以何种方式相互关联?了解该模式的结构后, 你可以更容易地理解下面基于真实世界的 php 应用案例。
index.php: 概念示例<?phpnamespace refactoringguru\adapter\conceptual;/** * the target defines the domain-specific interface used by the client code. */class target{ public function request(): string { return "target: the default target's behavior."; }}/** * the adaptee contains some useful behavior, but its interface is incompatible * with the existing client code. the adaptee needs some adaptation before the * client code can use it. */class adaptee{ public function specificrequest(): string { return ".eetpada eht fo roivaheb laiceps"; }}/** * the adapter makes the adaptee's interface compatible with the target's * interface. */class adapter extends target{ private $adaptee; public function __construct(adaptee $adaptee) { $this->adaptee = $adaptee; } public function request(): string { return "adapter: (translated) " . strrev($this->adaptee->specificrequest()); }}/** * the client code supports all classes that follow the target interface. */function clientcode(target $target){ echo $target->request();}echo "client: i can work just fine with the target objects:\n";$target = new target();clientcode($target);echo "\n\n";$adaptee = new adaptee();echo "client: the adaptee class has a weird interface. see, i don't understand it:\n";echo "adaptee: " . $adaptee->specificrequest();echo "\n\n";echo "client: but i can work with it via the adapter:\n";$adapter = new adapter($adaptee);clientcode($adapter);
output.txt: 执行结果client: i can work just fine with the target objects:target: the default target's behavior.client: the adaptee class has a weird interface. see, i don't understand it:adaptee: .eetpada eht fo roivaheb laicepsclient: but i can work with it via the adapter:adapter: (translated) special behavior of the adaptee.
推荐学习:《php视频教程》
以上就是详述php适配器模式(附代码示例)的详细内容。
其它类似信息

推荐信息