Adapter
[Link] name : Adapter : Adapter Design Pattern
Problem Description: Convert the interface of a legacy class into a different interface expected by the
client, so they can work together without changes.
[Link]:
Convert the interface of a class into another interface clients expect. Adapter lets classes
work together that couldn't otherwise because of incompatible interfaces.
[Link] Known As: Wrapper
[Link]:
Sometimes
⚫ toolkit class that's designed for reuse isn't reusable only because its
interface doesn't match the domain-specific interface an application requires.
Consider
⚫ for example a drawing editor that lets users draw and arrange graphical
elements (lines, polygons, text, etc.) into pictures and diagrams.
The
⚫ interface for graphical objects is defined by an abstract class called Shape.
Classes for elementary geometric shapes are easy to implement. But a
⚫ TextShape
subclass that can display and edit text is more difficult to implement. (screen updates,
buffer updates)
How
⚫ can existing and unrelated classes like TextView work in an application that
expects classes with a different and incompatible interface?
We
⚫ could define TextShape so that it adapts the TextView interface to Shape's.
(1) by inheriting Shape's interface and TextView's implementation or
(2) by composing a TextView instance within a TextShape and implementing
TextShape in terms of TextView's interface.
[Link]:
Use the Adapter pattern when
⚫
you want to use an existing class, and its interface does not match the one you need.
⚫
you want to create a reusable class that cooperates with unrelated or unforeseen classes,
that is, classes that don't necessarily have compatible interfaces.
⚫
(object adapter only) you need to use several existing subclasses, but it's impractical to
adapt their interface by subclassing every one. An object adapter can adapt the interface
of its parent class.
[Link]:
Object adapter
7..Participants:
Target
⚫ (Shape)
defines
the domain-specific interface that Client uses.
Client
⚫ (DrawingEditor)
collaborates
with objects conforming to the Target interface.
Adaptee
⚫ (TextView)
defines
an existing interface that needs adapting.
Adapter
⚫ (TextShape)
adapts the interface of Adaptee to the Target interface.
[Link]:
Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee
operations that carry out the request.
[Link]:
Class and object adapters have different trade-offs.
A class adapter
Adapts Adaptee to Target by committing to a concrete Adapter class.
lets Adapter override some of Adaptee's behavior, since Adapter is a subclass
of Adaptee.
introduces
only one object, and no additional pointer indirection is needed to
get to the adaptee.
An object adapter
lets
a single Adapter work with many Adaptees
makes it harder to override Adaptee behavior.
Pluggable adapters: describe classes with built-in interface adaptation.
Using
two-way adapters to provide transparency.
[Link]:
Here are some issues to keep in mind while implementing Adapter:
1. Implementing class adapters in C++. Adapter would inherit publicly from Target and
privately from Adaptee. Thus adapter would be a subtype of target but not of adaptee.
2. Pluggable adapters.
⚫
find a "narrow" interface for Adaptee
The narrow interface leads to three implementation approaches:
a. Using abstract operations: Sub classes must implements the
abstract operations and adapt the hierarchically structured object.
b. Using delegate objects. Tree displays forwards the requests for
accessing the hierarchical structure to a delegate object.
c. Parameterized adapters. The usual way to support pluggable
adapters in smalltalk is to parameterize an adapter with one or
more blocks. The block construct supports adaption without
subclassing.
[Link] Code:
class Shape {
public:
Shape();
virtual void BoundingBox(Point& bottomLeft,
Point& topRight) const; virtual Manipulator*
CreateManipulator() const;};
TextView();
voidGetOrigin(Coord& x, Coord& y)
const; voidGetExtent(Coord& width,
Coord& height) const; virtual bool
IsEmpty() const;
};
[Link] Uses: This pattern is used in the following toolkits: ET++Draw, InterViews
2.6, ObjectWorks\Smalltalk, NeXT'sAppKit
Related Patterns:
⚫
Bridge: has same structure but different intent.
⚫
Decorator: enhances another object without changing its interface.
⚫
Proxy: representative or surrogate for another object and does not change its
interface.
⚫
Factory Method is also a related pattern.
13,.Related patterns : Adapter can be similar to the remote form of Proxy. However,
Proxy doesn't change interfaces.
⚫
Decorator enhances another object without changing its interface.
⚫
Bridge similar structure to Adapter, but different [Link] interface from
implementation.