Adapter Design Pattern

melbournepatterns 1,271 views 10 slides May 07, 2009
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Presented by Russell Searle to the Melbourne Patterns Group


Slide Content

Adapter design pattern
Melbourne Design Patterns User Group
Russell Searle
6 May 2009

Structural patterns
Compose and assemble classes and objects
Structural class patterns inherit from adaptees
Adapter
Structural object patterns compose objects
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy

Adapter pattern
Converts a class interface into a different
interface expected by clients
Enables interoperation of unlike classes
aka: Wrapper
Useful in integration work, as a technique
and architectural principle

Why use adapter pattern?
You can’t reuse a candidate class because
its interface doesn’t match client or domain
requirements
You want to create a reusable class that
combines existing, unlike classes
You need to use existing subclasses but
you can’t subclass all of them

Class adapter
Uses single / multiple inheritance to transform interfaces
Implements a specific Adaptee class, so you can’t also
adapt the Adaptee’s subclasses
Can override Adaptee behaviour
Produces only one new object

Object adapter
Composes objects, uses delegation
Composition may be changed at run-time
Can implement or extend more than one
Adaptee class (if allowed)
May complicate overriding Adaptee behaviour

Design considerations
Complexity of adapter depends on scope
of function it adapts: integration work is
often very demanding
May design adapters for maximum reuse
by generalising its elements: pluggable
adapters
Two-way adapters to implement interfaces
from all sides of the problem domain, in
order to integrate them

Example

Related patterns
Bridge has similar structure, but is
intended to make an interface and its
implementation independent
Decorator elaborates another object
without changing its interface; can also
support recursion
Proxy produces a stand-in for another
object without changing its interface

Sources
Erich Gamma, Richard Helm, Ralph Johnson and John
Vlissides. Design patterns: elements of reusable object-
oriented software. Addison-Wesley, Reading, MA, 1995.
David Geary. Adopt Adapter. JavaWorld.com,
http://www.javaworld.com/javaworld/jw-09-2003/jw-0926-designpatterns.html
, 26 Sept 2003.