Title: Adapters
1Adapters
2Definition
- A pattern found in class diagrams in which you
are able to reuse an adaptee class by providing
a class, (the adapter) that delegates to the
adaptee. - AKA Wrapper
3Typical Context
- You are building an inheritance hierarchy and you
want to incorporate into it a class written by
somebody else - i.e. you want to reuse an
existing class. - Typically the methods of the reused class do not
have the same name or argument types as the
methods in the hierarchy you are creating. - The reused class is also often already part of
its own inheritance hierarchy.
4Problem
- How do you obtain the power of polymorphism when
reusing a class whose methods have the same
function but do not have the same signature as
the other methods in the hierarchy?
5Solution
- Rather than directly incorporating the reused
class into your inheritance hierarchy, instead
incorporate an Adapter class. - The Adapter is connected by an association to
the reused class, which we will call the
Adaptee. - The polymorphic methods of the Adapter delegate
to methods of the Adaptee.
6A Drawing Program Example
- Consider a drawing editor.
- Top level data type is the graphical object
- Subclasses of the graphical object class
LineShape, PolygonShape, TextShape etc.
7The TextShape Class
- The TextShape class is considerably more
difficult to implement than simple polygon
classes. - We have a user-interface toolkit which provides a
reusable TextView class for displaying and
editing text. - TextView was not developed with our shape-class
oriented design in mind.
Sounds like we need an adapter!
8Interface Differences
- TextView uses different names for its functions.
- TextView lacks the functionality for interactive
dragging of text etc, which our graphical
object requires.
9Adapters Solutions
- The Adapter provides our graphical object class
with functions under the expected name which
simply call the appropriate functions in
TextView. - The Adapter can contain additional functions such
as a TextManipulator which will be responsible
for the unsupported functionality.
10Structure
- Class Adapter
- Uses multiple inheritance to adapt one interface
to another. - Object Adapter
- Relies on object composition.
11Adapter Class Diagram(Class Adapter)
Target Request()
Adaptee SpecificRequest()
Client
(implementation)
Adapter Request()
SpecificRequest()
12(class) Adapter Class DiagramExample by Rob
Kremer, Professor of Computer Science at Calgary
University
We have a list
Target Request()
Adaptee SpecificRequest()
List InsertAtFront()Get Front()RemoveFront()
Queue Push()Pop()
Client
We want a queue
(implementation)
Adapter Request()
QueueImp Push()Pop()
SpecificRequest()
InsertAtFront()
Adapter to the rescue!
x GetFront()RemoveFront() return x
13Consequences (Class Adapter)
- The class adapter adapts adaptee to the target
interface by committing to a concrete adaptee
class. Thus a class adapter wont work when we
want to adapt a class and all its subclasses. - The adapter can override some of adaptees
behavior, since the adapter is a subclass of the
adaptee. - It introduces only one object, thus no additional
pointer indirection is needed to get to the
adaptee.
14Adapter Class Diagram(Object Adapter)
Target Request()
Adaptee SpecificRequest()
Client
adaptee
Adapter Request()
SpecificRequest()
Adaptee-gtSpecificRequest()
15(object) Adapter Class DiagramExample by Rob
Kremer, Professor of Computer Science at Calgary
University
Target Request()
Adaptee SpecificRequest()
Client
Use a pointer to an instance of the the adaptee
adaptee
Adapter Request()
SpecificRequest()
Adaptee-gtSpecificRequest()
Change the code of Request() to use the pointer
16Consequences (Object Adapter)
- Using an object adapter lets a single adapter
work with many adaptees, i.e.. the adapter itself
and all its subclasses. - It also makes it harder to override adaptee
behavior. Doing so will require subclassing
adaptee and making adapter refer to the subclass
instead of the adapter itself.
17Things to keep in mind for Adapters
- should be as general as possible
- should be described in an easy-to-understand form
so that people can determine when and how to use
it - should contain a solution that has been proven to
effectively solve the problem in the indicated
context - should be illustrated using a simple diagram
- should be written using a narrative writing style
18Adapters are one of the Gang of Four patterns
- Others include
- Facade
- Read-only interface
- Proxy
19References
- Design Patters Elements of Reusable
Object-Oriented Software, by Erich Gamma, Richard
Helm, Ralph Johnson, and John Vlissides - Object Oriented Software Engineering Knowledge
Base, http//www.site.uottawa.ca4321/oose/index.h
tml Knowledge base was created from the book
"Object Oriented Software Engineering Practical
Software Development using UML and Java by
Timothy C. Lethbridge and Robert Laganière
published by McGraw Hill in Summer 2001. - Diagrams taken from Prof Rob Kremers Notes from
Design Patterns Elements of Reusable
Object-Oriented Software, The Adapter and Bridge
Patterns http//sern.ucalgary.ca/courses/SENG/443
/W02/