Adapters - PowerPoint PPT Presentation

About This Presentation
Title:

Adapters

Description:

Typically the methods of the reused class do not have the same name or argument ... The class adapter adapts adaptee to the target interface by committing to a ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 20
Provided by: zacha6
Learn more at: http://www.cs.rpi.edu
Category:
Tags: adapters | class

less

Transcript and Presenter's Notes

Title: Adapters


1
Adapters
  • Presented By
  • Zachary Dea

2
Definition
  • 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

3
Typical Context
  1. 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.
  2. 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.
  3. The reused class is also often already part of
    its own inheritance hierarchy.

4
Problem
  • 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?

5
Solution
  1. Rather than directly incorporating the reused
    class into your inheritance hierarchy, instead
    incorporate an Adapter class.
  2. The Adapter is connected by an association to
    the reused class, which we will call the
    Adaptee.
  3. The polymorphic methods of the Adapter delegate
    to methods of the Adaptee.

6
A 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.

7
The 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!
8
Interface Differences
  1. TextView uses different names for its functions.
  2. TextView lacks the functionality for interactive
    dragging of text etc, which our graphical
    object requires.

9
Adapters Solutions
  1. The Adapter provides our graphical object class
    with functions under the expected name which
    simply call the appropriate functions in
    TextView.
  2. The Adapter can contain additional functions such
    as a TextManipulator which will be responsible
    for the unsupported functionality.

10
Structure
  • Class Adapter
  • Uses multiple inheritance to adapt one interface
    to another.
  • Object Adapter
  • Relies on object composition.

11
Adapter 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
13
Consequences (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.

14
Adapter 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
16
Consequences (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.

17
Things to keep in mind for Adapters
  1. should be as general as possible
  2. should be described in an easy-to-understand form
    so that people can determine when and how to use
    it
  3. should contain a solution that has been proven to
    effectively solve the problem in the indicated
    context
  4. should be illustrated using a simple diagram
  5. should be written using a narrative writing style

18
Adapters are one of the Gang of Four patterns
  • Others include
  • Facade
  • Read-only interface
  • Proxy

19
References
  • 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/
Write a Comment
User Comments (0)
About PowerShow.com