Adapter Design Pattern

Preview:

DESCRIPTION

Presented to Melbourne Design Patterns Group on 6 May 2009

Citation preview

Adapter design patternMelbourne Design Patterns User GroupRussell Searle6 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

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.

Recommended