development:software-architecture:design-patterns:adapter
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
development:software-architecture:design-patterns:adapter [2024/08/06 06:59] – created - external edit 127.0.0.1 | development:software-architecture:design-patterns:adapter [2024/08/14 13:48] (current) – [Tham khảo] tungnt | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Adapter ====== | ====== Adapter ====== | ||
- | ====== | + | Mẫu Adapter cho phép bạn sử dụng các lớp của bên thứ 3 hoặc lớp cũ ngay cả khi chúng không tương thích với phần lớn mã của bạn. |
+ | |||
+ | Ví dụ, thay vì viết lại giao diện thông báo của ứng dụng để hỗ trợ từng dịch vụ của bên thứ 3 như Slack, Facebook, SMS, bạn có thể tạo một bộ wrappers đặc biệt để điều chỉnh các cuộc gọi từ ứng dụng của bạn thành giao diện và định dạng mà từng lớp của bên thứ 3 yêu cầu. | ||
+ | |||
+ | <file php> | ||
+ | <?php | ||
+ | |||
+ | namespace RefactoringGuru\Adapter\RealWorld; | ||
+ | |||
+ | /** | ||
+ | * The Target interface represents the interface that your application' | ||
+ | * 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' | ||
+ | * SlackNotification doesn' | ||
+ | * extracting an interface should be your first step. | ||
+ | */ | ||
+ | class EmailNotification implements Notification | ||
+ | { | ||
+ | private $adminEmail; | ||
+ | |||
+ | public function __construct(string $adminEmail) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function send(string $title, string $message): void | ||
+ | { | ||
+ | mail($this-> | ||
+ | echo "Sent email with title ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 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-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | public function logIn(): void | ||
+ | { | ||
+ | // Send authentication request to Slack web service. | ||
+ | echo " | ||
+ | } | ||
+ | |||
+ | public function sendMessage(string $chatId, string $message): void | ||
+ | { | ||
+ | // Send message post request to Slack web service. | ||
+ | echo " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 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-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 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 | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The client code can work with any class that follows the Target interface. | ||
+ | */ | ||
+ | function clientCode(Notification $notification) | ||
+ | { | ||
+ | // ... | ||
+ | |||
+ | echo $notification-> | ||
+ | "< | ||
+ | "Our website is not responding. Call admins and bring it up!" | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | |||
+ | echo " | ||
+ | $notification | ||
+ | clientCode($notification); | ||
+ | echo " | ||
+ | |||
+ | |||
+ | echo "The same client code can work with other classes via adapter: | ||
+ | $slackApi | ||
+ | $notification | ||
+ | clientCode($notification); | ||
+ | </ | ||
- | * https:// |
development/software-architecture/design-patterns/adapter.1722927590.txt.gz · Last modified: 2024/08/06 06:59 by 127.0.0.1