Breaking down the Adapter pattern
As part of the Structural family of design patterns, the Adapter pattern is all about compatibility, turning classes that don’t naturally work together because of incompatible interfaces into coworkers that your client code can handle. Think of the charging plug for your phone – if you tried to use a cord with the wrong plug, you couldn’t get the phone to connect and charge. But with an adapter acting as a middleman, you could get the electricity running and your phone charged without having to buy a whole new device (or redo the entire electrical system in your home, which is always a plus).
The Adapter pattern is useful when:
- You want to use an existing class with an interface that doesn’t match the one your client uses.
- You need to create reusable classes that can work with unrelated (and future) classes that may not have compatible interfaces.
- You have existing subclass hierarchies that would...