16

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 ...

Get Learning Design Patterns with Unity now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.