Adapter Design Pattern in PHP: A Real-World Example
The Adapter pattern is a structural design pattern that allows objects with incompatible interfaces to collaborate. It acts as a wrapper between two objects, converting calls from one object to a format recognizable by the other object.
Real-world use case
One real-world use case of the Adapter pattern is in a system that needs to send emails using different email providers. For example, a small business may need to send emails using SendInBlue, Mailgun, and Gmail. Each of these email providers has a different API, so the small business’s system cannot send emails to them directly.
To solve this problem, the small business can use the Adapter pattern to create adapters for each email provider. Each adapter will convert the call to send an email to the format required by the corresponding email provider.
The following PHP code shows an example of the Adapter pattern for sending emails:
class SendInBlueEmailProvider
{
public function notify()
{
print_r("Send SendInBlue Email \n");
}
}
interface IEmailAdapter {
public function notify();
}
class MailgunEmail
{
public function sendEmail()
{
print_r("Send Mailgun Email \n");
}
}
class MailgunEmailAdapter implements IEmailAdapter
{
public function __construct(public MailgunEmail $email){}
public function notify()
{
return $this->email->sendEmail();
}
}
$oldProvider = (new SendInBlueEmailProvider);
$mailgun = new MailgunEmail;
$mailgunAdapter = (new MailgunEmailAdapter($mailgun));
$oldProvider->notify(); // Send SendInBlue Email
$mailgunAdapter->notify(); // Send Mailgun Email
Output
When we run the above code, the following output is produced:
Send SendInBlue Email
Send Mailgun Email
Pros and cons of the Adapter pattern
The Adapter pattern has several advantages:
- It allows objects with incompatible interfaces to collaborate.
- It makes your code more flexible and reusable.
- It can help to decouple your code from third-party libraries or legacy systems.
However, the Adapter pattern also has some disadvantages:
- It can add complexity to your code.
- It can be difficult to maintain multiple adapters, especially if the interfaces of the adapted objects change frequently.
Overall, the Adapter pattern is a useful tool for integrating incompatible interfaces. It is important to weigh the pros and cons before using it in your code.
Conclusion
The Adapter design pattern is a powerful tool that can be used to integrate incompatible interfaces. It is often used to integrate legacy systems with new systems, or to use third-party libraries that have different interfaces than your own system. The Adapter pattern can also be used to adapt classes to different interfaces for testing purposes.
While the Adapter pattern has some advantages, such as flexibility and reusability, it can also add complexity to your code and be difficult to maintain multiple adapters. It is important to weigh the pros and cons before using the Adapter pattern in your code.
N.B: Example of code in PHP is my only contribution here, rest of the things is generated from Google Bard.
You can follow me on