Pattern:
Adapter - Class


Author

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

Intent

"Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldnt otherwise because of incompatible interfaces." (Gamma, E., R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Reading, MA: Addison-Wesley, 1995)

Motivation

Sometimes a toolkit class thats designed for reuse isnt reusable only because its interface doesnt match the domain-specific interface an application requires. Consider a drawing editor that lets users draw and arrange graphical elements (lines, polygons, text, etc.) into pictures and diagrams. The drawing editors key abstraction is the graphical object, which has an editable shape and can draw itself. The interface for graphical objects is defined by an abstract class called Shape (Target in the thumbnail). The editor defines a subclass of Shape for each kind of graphical object: a LineShape class for lines, a PolygonShape class for polygons, and so forth. Classes for elementary geometric shapes like LineShape and PolygonShape are rather easy to implement, because their drawing and editing capabilities are inherently limited. But a TextShape subclass (Adapter in the thumbnail) that can display and edit text is considerably more difficult to implement, since even basic text editing involves complicated screen update and buffer management. Meanwhile, an off-the-shelf user interface toolkit might already provide a sophisticated TextView (Adaptee in the thumbnail) class for displaying and editing text. We could define TextShape so that it adapts the TextView interface to Shapes. We can do this in one of two ways: (1) by inheriting Shapes interface and TextViews implementation or (2) by composing a TextView instance within a TextShape and implementing TextShape in terms of TextViews interface. These two approaches correspond to the class and object versions of the Adapter pattern. TextShape is an adapter

Known Uses

Meyers "Marriage of Convenience" is a form of class adapter. Meyer describes how a FixedStack class adapts the implementation of an Array class to the interface of a Stack class. The result is a stack containing a fixed number of entries. (Meyer, B. Object-Oriented Software Construction. Series in Computer Science. Prentice Hall, Englewood Cliffs, NJ, 1988.)

Thumbnail

Keywords

Adapter pattern, structural patterns, Gamma patterns, Adapter Class pattern, Adapter Object pattern, Wrapper pattern, interfaces (existing, incompatible), pluggable adapters, forward requests

Business Domains

any system integrated with multiple, disparate systems

Problem Forces

need a class that is reusable in multiple, unforeseen contextsneed to de-couple a class and its implementationneed to use an existing class but the interface is not compatible with another class

Benefits

increases reuse of classes by de-coupling the interface from the implementation

Implementation Files

Test driver Test.javaSimple implementation of the pattern - Target class Target.javaSimple implementation of the pattern - Adapter class Adapter.javaSimple implementatio of the pattern - Adaptee class Adaptee.javaTest driver testclassadapter.cppSimple implementation of the pattern. classadapter.h



Generated on Fri Oct 20 10:27:33 GMT+02:00 2000 by Framework Studio