0% found this document useful (0 votes)
2 views9 pages

Chapter19 Adapter

Uploaded by

mvanheerdentuks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Chapter19 Adapter

Uploaded by

mvanheerdentuks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Computer Science

Tackling Design Patterns


Chapter 19: Adapter Design Pattern
Copyright c 2016 by Linda Marshall and Vreda Pieterse. All rights reserved.

Contents
19.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

19.2 Adapter Design Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . 2


19.2.1 Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
19.2.2 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
19.2.3 Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
19.2.4 Participants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

19.3 Protected and private inheritance in C++ explained . . . . . . . 3

19.4 Adapter Pattern Explained . . . . . . . . . . . . . . . . . . . . . . . . 5


19.4.1 Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
19.4.2 Comparison of the approaches . . . . . . . . . . . . . . . . . . . . . . . 5
19.4.3 Real world example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
19.4.4 Related Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

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 Adapter Design Pattern

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.

Figure 1: The structure of the Object Adapter Design Pattern

2
Figure 2: The structure of the Class Adapter Design Pattern

19.2.4 Participants
Adaptee

• The existing interface that needs to be adapted

Target

• Domain specific interface used by the client

Adapter

• Adapts the interface of Adaptee to the Target interface

Client

• Manipulates objects conforming to the interface specified by the abstract class


Target

19.3 Protected and private inheritance in C++ ex-


plained
So far, the member access specifier (memberAccessSpecifier in Figure 3) for inheritance
has been public. Two other member access specifiers for inheritance may be used, namely
protected and private. Table 1 provides the visibility of the members of the base class
in the derived class for each of the member access specifiers.
It can be said that a memberAccessSpecifier that is private provides the derived class
with the functionality defined in the base class. Effectively, the base class is wrapped and
no class that may inherit from the derived class will have access to its member functions.
Private inheritance in C++ can be seen as a type of has-a relationship.

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 {
...
};

Figure 3: Example inheritance classes

Inheritance access specifier of derived class


public protected private
public Derived access speci- Derived access speci- Derived access speci-
fier is public. Derived fier is protected. De- fier is private. De-
class can access the rived class can access rived class can access
member and so can an the member, but there the member, but there
outside class. is no access from an is no access from an
Base member visibility

outside class. outside class.


protected Derived access speci- Derived access speci- Derived access speci-
fier is protected. De- fier is protected. De- fier is private. De-
rived class can access rived class can access rived class can access
the member, but there the member, but there the member, but there
is no access from an is no access from an is no access from an
outside class. outside class. outside class.
private Derived access speci- Derived access speci- Derived access speci-
fier is private. De- fier is private. De- fier is private. De-
rived class cannot ac- rived class cannot ac- rived class cannot ac-
cess the member and cess the member and cess the member and
there is no access from there is no access from there is no access from
an outside class. an outside class. an outside class.

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.4.2 Comparison of the approaches


When does one use delegation and when does one use private inheritance. Try to always
use delegation. Use composition (inheritance) only when necessary [1].

19.4.3 Real world example


Many instances of the adapter pattern can be found in data structures where one data
structure such as a list is wrapped so that it behaves as another data structure, for example
a stack.
Further use of the Adapter pattern is when legacy systems are being integrated into a
new system. The functionality of the legacy system is therefore encapsulated and used
by the new system through an adapter.

19.4.4 Related Patterns


Bridge
Structurally they are similar. However their intent is different, the Adapter changes
the interface while the Bridge separates the implementation from the interface.
Decorator
Enhances an object without changing the interface.
Proxy
Defines a surrogate of to an object without changing its interface.

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.

Figure 4: Billboard example - object adapter

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.

Figure 5: Billboard example - class adapter

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 ;
};

Listing 2: Class 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 , private E l e c t r o n i c B i l l b o a r d {
public :
TrafficBillboard () {
updateMessage ( ” 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 ( ) {
onof ( ) ;
};

v i r t u a l void changeMessage ( int msgId ) {


switch ( msgId ) {
case 1 : updateMessage ( ” slow t r a f f i c ahead ” ) ; break ;
case 2 : updateMessage ( ” a c c i d e n t ahead ” ) ; break ;
default : updateMessage ( ” a l l c l e a r ” ) ;
}

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.

Figure 6: Rectangle example - class adapter

Listing 3: Rectangle Class Adapter Implementation


c l a s s RectangleAdapter : public Rectangle ,
private L e g a c y R e c t a n g l e {
public :
Re ct angleAdapter ( C o o r d i n a t e x , C o o r d i n a t e y ,
Dimension w, Dimension h )
: L e g a c y R e c t a n g l e ( x , y , x+w, y+h ) {

c o u t << ” RectangleAdapter : c r e a t e . ( ” << x


<< ” , ” << y

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.

2. Rewrite the example given in Section 19.5.2 as an object adapter.

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.

[3] Vince Huston. Design patterns. http://www.cs.huji.ac.il/labs/parallel/Docs/


C++/DesignPatterns/, n.d. [Online: Accessed 29 June 2011].

[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.

You might also like