Converting Incompatible Classes with the Adapter Pattern
In the last chapter, we used the Decorator pattern to dynamically add new behavior to existing classes without using a traditional subclassing approach. In this chapter, we’ll stick with the idea of adding new behavior without touching an existing class, but this time, we’ll use interfaces to adapt existing incompatible classes to work together using the Adapter pattern (which is perfect when you need to force two unruly systems to play together nicely without overhauling either one).
Now, the Adapter pattern can be used for all kinds of situations, including porting an existing class into an interface your client code already uses, creating reusable classes that work with classes you haven’t even built yet, or bypassing a deep subclass hierarchy by adapting a single parent class. You can even use the pattern to converge properties from classes that don’t share any common class hierarchies! While...