blog

Discover the Adapter Design Pattern Illustrated in PHP

Share:

An adapter facilitates the conversion or translation of one interface for compatibility with another.

Assuming you’ve grasped the adapter design pattern from the image above, let’s delve deeper.

The Adapter Design Pattern is a structural pattern that allows objects with incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces. In PHP, you can implement the Adapter pattern using classes and interfaces.

Let’s understand the Adapter pattern with an example:

Suppose you have an existing Book class with a method open():

Now, you have a new requirement to create an eReader class that should have a method turnOn() instead of open(). However, you can’t change the existing Book class. Here, you can use the Adapter pattern to make Book compatible with the new interface.

First, define the interface for the eReader:

Now, create an adapter class BookAdapter that implements the EReaderInterface and internally uses the Book class:

Now, you can use the BookAdapter with the EReaderInterface:

In this example, BookAdapter acts as a bridge between the Book class and the EReaderInterface, allowing the Book class to work with the new interface without modifying its code.

If you are familiar with Laravel and used file system you see you can use many driver like local, Amazon S3 and so on.

Hoping you found this helpful 😉

Related articles

Circle icon
Circle icon
Circle icon
Circle icon
Circle icon
Circle icon
Circle icon
Circle icon
Circle icon
Circle icon
Circle icon
Circle icon

get in touch

EVEN IF YOU DON'T YET KNOW WHERE TO START WITH YOUR PROJECT - THIS IS THE PLACE

Drop us a few lines and we'll get back to you within one business day.

Thank you for your inquiry! Someone from our team will contact you shortly.
Where from have you heard about us?
Clutch
GoodFirms
Crunchbase
Googlesearch
LinkedIn
Facebook
Your option
I have read and accepted the Terms & Conditions and Privacy Policy
bracket icon
bracket icon
bracket icon
bracket icon
bracket icon
bracket icon
slash icon
slash icon
slash icon
slash icon
slash icon
slash icon
bracket icon
bracket icon
bracket icon
bracket icon
bracket icon
bracket icon