Class Adapter

Intent

Convert the interface of a class into another interface clients expect.

product

Applicability

You obtain a target class, which you need to use in your designs, but, the public interface of the target class (it’s methods) does not match your requirements or it does not fit into the necessary class inheritance hierarchy.

Solution

  1. Create an abstract interface ancestor class that implements the interchangeable interface that clients expect
  2. Create an adapter subclass of the interface class.
  3. Multiply inherit the adapter class from the target class to automatically include both its implementation. Note: in C++ this inheritance is typically declared as private.
  4. Override the methods from the interface class in the adapter class to call the methods in the target class.

Consequences

Related Patterns

Object Adapter provides an alternate solution that does not use multiple inheritance


Quick Pick