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 😉