Chapter 5. Adapter Pattern

Adapt or perish, now as ever, is nature’s inexorable imperative.

H. G. Wells

The presentations and conceptions of the average man of the world are formed and dominated, not by the full and pure desire for knowledge as an end in itself, but by the struggle to adapt himself favorably to the conditions of life.

Ernst Mach

What is the Adapter Pattern?

The basic motivation for the adapter pattern is to enable wider use of existing classes. For example, let’s assume that there’s a class that does a particular job, and does it well. We want to use this class in an application, but it just doesn’t fit all the current requirements. We may want to expand its features, or combine it with some other classes to provide additional functionality. The bottom line is that we must adapt this existing class to fit new requirements. This is what the adapter pattern does; it allows a client to use an existing class by converting its interface into one that fits a new context. The key point to remember is that the existing class isn’t modified, but an adapter class that has the right interface uses it or extends it to provide the necessary functionality.

A Design Pattern for Potty Training

A good example of an adapter is the toilet seat adapter used by toddlers that fits on top of a traditional toilet seat. Let’s take a look at the context. We have a legacy object that is the toilet seat, whose basic design hasn’t changed in years. It functions well for its original adult users ...

Get ActionScript 3.0 Design Patterns 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.