Chapter19 Adapter
Chapter19 Adapter
Contents
19.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
19.5 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
19.5.1 Billboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
19.5.2 Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
19.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
19.1 Introduction
This Lecture Note introduces the Adapter design pattern. The adapter pattern is also
referred to as a wrapper pattern which describes it intent very well. This wrapper is
presented in two guises, the first adapts using delegation and the other using inheritance.
Both these implementation strategies will be considered. Due to the nature of the inher-
itance, inheritance access specification other then public will be briefly discussed.
19.2.1 Identification
Name Classification Strategy
Adapter Structural Inheritance (Class) and
Delegation (Object)
Intent
Convert an interface of a class into another interface clients expect. Adapter lets
classes work together that couldn’t otherwise because of incompatible interfaces.
([2]:139)
19.2.2 Problem
Used to modify existing interfaces to make it work after it has been designed.
19.2.3 Structure
The Adapter design pattern is the only pattern to which one of two structures can be
applied. The pattern can either make use of delegation or inheritance to achieve its intent.
The delegation structure is referred to as an Object Adapter, Figure 1, and the inheritance
structure as shown in Figure 2 for the Class Adapter.
2
Figure 2: The structure of the Class Adapter Design Pattern
19.2.4 Participants
Adaptee
Target
Adapter
Client
3
c l a s s Base {
...
};
c l a s s Derived : m e m b e r A c c e s s S p e c i f i e r Base {
...
};
Table 1: C++ member access specifiers and base class member visibility
4
19.4 Adapter Pattern Explained
19.4.1 Design
Object Adapter
Object Adapter makes use of object composition to delegate to Adaptee.
Class Adapter
Class Adapter makes use of mixin idiom [4]. A mixin is an object-orientated concept
by which a class provides functionality, either to be inherited or just used, but is
not explicitly instantiated. Adapter inherits and implements Target (public inher-
itance). Adapter inherits only the implementation, or functionality, and therefore
the use of private inheritance of Adaptee resulting in a linearisation of the hierarchy.
19.5 Example
19.5.1 Billboard
In this example, the electronic billboard is to be adapted to in order to simplify its
interface. Electronic billboards can be in one of two states, either on or off with the ability
5
to toggle between the states. Electronic billboards in the on state display a message, to
change this message another setter method is called and then the method to display needs
to be called for the message to change.
The Billboard interface (Figure 4) simplifies the the electronic billboard and TrafficBillboard
implements this simplified interface and uses the ElectronicBillboard functionality to
do so.
Implementing the Billboard as a class adapter instead of as an object adapter is not
difficult. Figure 5 shows the resulting class diagram.
By comparing Figures 4 and 5 it can be seen that the most significant difference between
the two implementations is that the member attribute, providing the delegation function-
ality, is not defined in the class adapter. In order to see other subtle differences, it is
necessary to look at the implementation level. Listings 1 and 2 represent the Adapter
participant of the pattern for the Object Adapter and Class Adapter implementations
respectively. The Object Adapter implementation instantiates an object of the Adaptee
participant and access the functionality provided by the object through its public inter-
face. The Class Adapter implementation, through private inheritance, uses and effectively
6
subsumes the functionality of the Adaptee participant. It is important to note, that had
there been protected features in the Adaptee participant, these would have been avail-
able to the Adapter participant with the Class Adapter implementation and not with the
Object Adapter implementation.
Listing 1: Object Adapter Implementation of the Billboard
c l a s s T r a f f i c B i l l b o a r d : public B i l l b o a r d {
public :
TrafficBillboard () {
board = new E l e c t r o n i c B i l l b o a r d ( ” a l l c l e a r ” ) ;
};
v i r t u a l void c h a n g e S t a t e ( ) {
board−>o n o f ( ) ;
};
v i r t u a l void changeMessage ( int msgId ) {
switch ( msgId ) {
case 1 : board−>updateMessage ( ” slow t r a f f i c ahead ” ) ; break ;
case 2 : board−>updateMessage ( ” a c c i d e n t ahead ” ) ; break ;
default : board−>updateMessage ( ” a l l c l e a r ” ) ;
}
};
v i r t u a l void d i s p l a y M e s s a g e ( ) {
i f ( board−>i s o n ( ) ) {
cout << ” T r a f f i c warning : ” ; board−>updateBoard ( ) ;
cout<<e n d l ;
}
e l s e cout<<” Board i s o f f ”<<e n d l ;
};
private :
E l e c t r o n i c B i l l b o a r d ∗ board ;
};
v i r t u a l void c h a n g e S t a t e ( ) {
onof ( ) ;
};
7
};
v i r t u a l void d i s p l a y M e s s a g e ( ) {
i f ( ison ()) {
cout << ” T r a f f i c warning : ” ; updateBoard ( ) ;
cout<<e n d l ;
}
e l s e cout<<” Board i s o f f ”<<e n d l ;
};
};
19.5.2 Rectangle
This example is available on the internet in different guises [3]. This is yet another
adaptation along the same theme and illustrates the implementation of a class adapter.
The class diagram for the rectangle is given in Figure 6 and the implementation of the
Adapter participant in listing 3. LegacyRectangle specifies a rectangle by using 4 values,
the first two values represent the x and y coordinates of the top left corner of the rectangle
and the last two values the x and y coordinates of the bottom right corner of a rectangle.
The adapted rectangle defines a rectangle by its top left corner coordinates and then a
width and a height value towards the right and down.
8
<< ” ) , width = ” << w
<< ” , h e i g h t = ” << h << e n d l ;
}
v i r t u a l void draw ( ) {
c o ut << ” RectangleAdapter : draw . ” << e n d l ;
oldDraw ( ) ; }
};
19.6 Exercises
1. Make use of the state design pattern to encapsulate the messages displayed by the
traffic billboard.
References
[1] Marshall Cline. C++ faq: Inheritance - private and protected inheritance, 1991–
2011. URL http://www.parashift.com/c++-faq-lite/private-inheritance.
html. Online; accessed 26 September 2011.
[2] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design patterns :
elements of reusable object-oriented software. Addison-Wesley, Reading, Mass, 1995.
[4] Yannis Smaragdakis and Don S. Batory. Mixin-based programming in c++. In Pro-
ceedings of the Second International Symposium on Generative and Component-Based
Software Engineering-Revised Papers, GCSE ’00, pages 163–177, London, UK, 2001.
Springer-Verlag. ISBN 3-540-42578-0. URL http://dl.acm.org/citation.cfm?id=
645417.652070.