Structural Design Patterns

Adapter

Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Also known as: Wrapper


Use the Adapter pattern when the correct class already exists but has the wrong interface. Typically used when the existing class will be a derived class and so it has to match the interface prescribed in the base class.

Implementation:

class Circle : public Shape 
{ 
   . . . 
   
    private: 
        ExistingCircle * pxc; 

   . . .        
};

Circle::Circle()
{ 
   . . . 
       pxc = new ExistingCircle(); 
} 

Circle::display()
{ 
       pxc->displayShape(); 
}