Adapter Design Pattern


Adapter Design Pattern

Maria
Alright, today we’re diving into the adapter design pattern. You might have heard it also called a wrapper, and that name gives you a big hint about what it does.

Michael
A wrapper — so it wraps something up. But before we get into details, let’s quickly place it among those other three patterns that get confused with it: facade, proxy, and decorator. We already covered decorator, so adapter is next.

Maria
Right. And the key idea for adapter is simple: it makes two incompatible interfaces compatible. Imagine you have a client that expects a certain interface, but you have a class with a completely different one. The adapter sits in between, translating calls so they can work together.

Michael
So it’s like a travel adapter for your electronics. You plug a US plug into the adapter, and the adapter fits into a European socket. The adapter just converts the shape, not the electricity. Same concept here.

Maria
Exactly. Let’s do a software example. Say your system has a `ReportGenerator` interface with a method `generateReport()`. But you just bought a third-party library that has a class `FancyReporter` with a method `createFancyDocument()`. Without changing either side, you write an adapter class that implements `generateReport()` and internally calls `createFancyDocument()`. Now they work together seamlessly.

Michael
That’s the wrapper aspect — the adapter wraps the third-party object and presents the expected interface. And this pattern is especially useful when you’re integrating legacy code or external libraries that you can’t modify.

Maria
Yes. And that’s the core of the adapter pattern: make things work together without rewriting them. In the next video we’ll talk about the facade pattern, which sounds similar but has a different goal.

Ask anything
We've read this whole study set, so questions about it get the best answers.