Introducing Adapter Pattern0:00
You'll sometimes find yourself in the position of needing to take one interface or one public API that you don't control, and you need to adapt that to a different interface altogether. So in these situations, the key word was adapt, so we can use the adapter pattern. So let me show you a simple example, my favorite simple example, and then we'll review a couple real-life use cases. Okay, so imagine that you have a paper book. And we'll set this up. So we have our paper book, and the public API for that would be maybe something to open and to turn the page, right? So we're just going to stub this out by saying var_dump, open the book, and then another one for turn the page. All right, very simple. So what we can see here is that we don't have an interface yet,
PaperBook API Example0:47
All right, very simple. So what we can see here is that we don't have an interface yet, but our implicit API or the implicit interface is that we have an open and turnPage method that users can call. So let's try it out. PaperBook, and then we'll say new it up, and then open it, and then finally turnPage. Really basic stuff. So if we run it, sure enough, we get those two. Okay, but now imagine that you have this different format altogether like an eBook. Okay, so now we're going to have a Kindle here, and it's going to be similar to our PaperBook because you can still use it to read,
Building an eBook Adapter2:13
and adapt it to another one, like paper book. Okay, this is what an adapter is for. So we could set up an e-book adapter. Let's set this up, namespace app, and now we're going to have our e-book adapter. And once again, this is going to be responsible for adapting the interface of an e-book to one that we require, which would be a paper book. Okay, so we're going to accept whatever e-book it is. It could be a Kindle, it could be something different altogether. And we'll assign that.
Defining Book Interfaces4:05
many of these patterns were popularized in the Java community, so you're probably going to see interfaces in the examples as well, and that's completely fine if you want to put that in your PHP. So in that case, you would see maybe a dedicated Book interface, and let's steal from eBook to be fast. This would be an interface for a Book that would consist of these two methods, and then you would have another interface for your eBook. So once again, let's grab that. This would be eBook, and that interface is start and clickNext, right? Yeah, that would be the only difference.
Real-World Adapter Use Cases5:36
So we have all of these different strategies and ways of doing basically the same thing, writing a file, getting a file, reading a file. However, you probably need adapters to make everything work. So if you're using S3, you probably need an adapter to work with maybe an S3 package. So you take whatever API their SDK exposes, and then you make that conform to whatever interface you want. And then you would do the same thing for Dropbox, or Box, or file system, or anything you want there. Because maybe what you decide is that... Actually, let's close this out.
