Adapter comes in two variants; one based on multiple inheritance:
and one making use of delegation:
Convert the interface of a class into another interface clients expect. Adapter lets
classes work together that couldn't otherwise because of incompatible interfaces.
(See the STL container adaptors
for a good example of adapting template containers.)
- Need to using an existing class, but its interface doesn't match
- Need to make use of incompatible classes
- (object adapter) Need to use several existing subclasses, but don't what to
subclass them all
- Class adapter commits to the concrete Adapter class: it won't work with Adapter's
subclasses, but object adapter does.
- Class adapter introduces only one object and no ponter indirection
- Object adapter requires quite a bit of work, since all of Adaptee's interface must