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.

- Wrap an existing class with a new interface.

- Impedance match an old component to a new system

Application

- An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed.

- Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.

Ckecklist

- Identify the players: the component(s) that want to be accommodated (i.e. the client), and the component that needs to adapt (i.e. the adaptee).

- Identify the interface that the client requires.

- Design a “wrapper” class that can “impedance match” the adaptee to the client.

- The adapter/wrapper class “has a” instance of the adaptee class.

- The adapter/wrapper class “maps” the client interface to the adaptee interface.

- The client uses (is coupled to) the new interface

Thumb Rules

- Adapter makes things work after they’re designed; Bridge makes them work before they are.

- Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together.

- Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.

- Adapter is meant to change the interface of an existing object. Decorator enhances another object without changing its interface. Decorator is thus more transparent to the application than an adapter is. As a consequence, Decorator supports recursive composition, which isn’t possible with pure Adapters.

- Facade defines a new interface, whereas Adapter reuses an old interface. Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.

Code

#include <iostream.h>

typedef int Coordinate;

typedef int Dimension;

// Desired interface

class Rectangle

{

public:

virtual void draw() = 0;

};

// Legacy component

class LegacyRectangle

{

public:

LegacyRectangle(Coordinate x1, Coordinate y1, Coordinate x2, Coordinate y2)

{

x1_ = x1;

y1_ = y1;

x2_ = x2;

y2_ = y2;

cout << "LegacyRectangle: create. (" << x1_ << "," << y1_ << ") => ("

<< x2_ << "," << y2_ << ")" << endl;

}

void oldDraw()

{

cout << "LegacyRectangle: oldDraw. (" << x1_ << "," << y1_ <<

") => (" << x2_ << "," << y2_ << ")" << endl;

}

private:

Coordinate x1_;

Coordinate y1_;

Coordinate x2_;

Coordinate y2_;

};

// Adapter wrapper

class RectangleAdapter: public Rectangle, private LegacyRectangle

{

public:

RectangleAdapter(Coordinate x, Coordinate y, Dimension w, Dimension h):

LegacyRectangle(x, y, x + w, y + h)

{

cout << "RectangleAdapter: create. (" << x << "," << y <<

"), width = " << w << ", height = " << h << endl;

}

virtual void draw()

{

cout << "RectangleAdapter: draw." << endl;

oldDraw();

}

};

int main()

{

Rectangle *r = new RectangleAdapter(120, 200, 60, 40);

r->draw();

system("PAUSE");

}

LegacyRectangle: create. (120,200) => (180,240)

RectangleAdapter: create. (120,200), width = 60, height = 40

RectangleAdapter: draw.

LegacyRectangle: oldDraw. (120,200) => (180,240)