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.

Adapter Design Pattern

  • 1.
    Adapter design patternMelbourne Design Patterns User Group Russell Searle 6 May 2009
  • 2.
    Structural patterns Composeand assemble classes and objects Structural class patterns inherit from adaptees Adapter Structural object patterns compose objects Adapter Bridge Composite Decorator Façade Flyweight Proxy
  • 3.
    Adapter pattern Convertsa 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
  • 4.
    Why use adapterpattern? 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
  • 5.
    Class adapter Usessingle / 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
  • 6.
    Object adapter Composesobjects, 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
  • 7.
    Design considerations Complexityof 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
  • 8.
  • 9.
    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
  • 10.
    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.