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

Adapter Design Pattern

Embed Size (px)

DESCRIPTION

Presented to Melbourne Design Patterns Group on 6 May 2009

Citation preview

Page 1: Adapter Design Pattern

Adapter design patternMelbourne Design Patterns User GroupRussell Searle6 May 2009

Page 2: Adapter Design Pattern

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

Page 3: Adapter Design Pattern

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

Page 4: Adapter Design Pattern

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

Page 5: Adapter Design Pattern

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

Page 6: Adapter Design Pattern

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

Page 7: Adapter Design Pattern

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

Page 9: Adapter Design Pattern

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

Page 10: Adapter Design Pattern

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.