The Adapter Design Pattern is a structural pattern that allows objects with incompatible interfaces to work together. It acts as an intermediary (or adapter) between two objects, converting the interface of one object to the interface expected by the other. This allows classes that would otherwise be incompatible because they have different interfaces to cooperate without modifications to their original code.
Adapter Structure
The Adapter pattern is generally composed of three main elements:
Types of Adapters
When to use the Adapter?
This pattern is useful in systems that need to work with external libraries or APIs, allowing you to adapt their functionality without changing the code of these libraries.
Here is an example of how to use the Adapter Design Pattern to integrate PHPMailer with a custom interface.
Situation:
Let's assume that your system expects any email sending class to implement an interface called IMailer, but PHPMailer does not follow this interface directly. The Adapter will be used to adapt PHPMailer to the interface expected by the system.
Install PHPMailer via Composer
composer require phpmailer/phpmailer
Directory System
?Adapter ┣ ?src ┃ ┣ ?Interfaces ┃ ┃ ┗ ?IMailer.php ┃ ┣ ?Adapters ┃ ┃ ┗ ?PHPMailerAdapter.php ┃ ┗ ?Services ┃ ┗ ?ServicoDeEmail.php ┣ ?vendor ┣ ?composer.json ┗ ?index.php
Autoload
In the composer.json file (located at the root of the project), add the App namespace to automatically load the classes:
{ "autoload": { "psr-4": { "App\\": "src/" } }, "require": { "phpmailer/phpmailer": "^6.5" } }
Interface IMailer
namespace App\Interfaces; interface IMailer { public function send($to, $subject, $message); }
Class PHPMailerAdapter
namespace App\Adapters; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; use App\Interfaces\IMailer; class PHPMailerAdapter implements IMailer { private $phpMailer; public function __construct() { $this->phpMailer = new PHPMailer(true); // Basic PHPMailer configuration $this->phpMailer->isSMTP(); $this->phpMailer->Host = 'smtp.example.com'; $this->phpMailer->SMTPAuth = true; $this->phpMailer->Username = '[email protected]'; $this->phpMailer->Password = 'password'; $this->phpMailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $this->phpMailer->Port = 587; $this->phpMailer->setFrom('[email protected]', 'Your Name'); } }
public function send($to, $subject, $message) { try { $this->phpMailer->addAddress($to); $this->phpMailer->Subject = $subject; $this->phpMailer->Body = $message; $this->phpMailer->send(); echo 'Email sent successfully!'; } catch (Exception $e) { echo "Failed to send email: {$this->phpMailer->ErrorInfo}"; } }
Class EmailService
namespace App\Services; use App\Interfaces\IMailer; class EmailService { private $mailer; public function __construct(IMailer $mailer) { $this->mailer = $mailer; } }
public function sendEmailToClient($to, $subject, $message) { $this->mailer->send($to, $subject, $message); }
File index.php
require 'vendor/autoload.php'; use App\Adapters\PHPMailerAdapter; use App\Services\EmailService; // Creating the PHPMailer Adapter $mailer = new PHPMailerAdapter(); // Using the email service with the IMailer interface $emailService = new EmailService($mailer); // Sending an email $emailService->sendEmailToClient('[email protected]', 'Email Subject', 'Message content.');
Explanation of the Structure
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3