Adapter - The Adapter Pattern Design Patterns In Java Bob...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Design Patterns In Java Bob Tarr The Adapter Pattern
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bob Tarr Design Patterns In Java The Adapter Pattern 2 The Adapter Pattern The Adapter Pattern l Intent Ø Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. l Also Known As Ø Wrapper l Motivation Ø Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application Ø We can not change the library interface, since we may not have its source code Ø Even if we did have the source code, we probably should not change the library for each domain-specific application
Background image of page 2
Bob Tarr Design Patterns In Java The Adapter Pattern 3 The Adapter Pattern The Adapter Pattern l Motivation Ø Example:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bob Tarr Design Patterns In Java The Adapter Pattern 4 The Adapter Pattern The Adapter Pattern l Structure Ø A class adapter uses multiple inheritance to adapt one interface to another:
Background image of page 4
Bob Tarr Design Patterns In Java The Adapter Pattern 5 The Adapter Pattern The Adapter Pattern l Structure Ø An object adapter relies on object composition:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bob Tarr Design Patterns In Java The Adapter Pattern 6 The Adapter Pattern The Adapter Pattern l Applicability Use the Adapter pattern when Ø You want to use an existing class, and its interface does not match the one you need Ø You want to create a reusable class that cooperates with unrelated classes with incompatible interfaces l Implementation Issues Ø How much adapting should be done? Simple interface conversion that just changes operation names and order of arguments Totally different set of operations Ø Does the adapter provide two-way transparency? A two-way adapter supports both the Target and the Adaptee interface. It allows an adapted object (Adapter) to appear as an Adaptee object or a Target object
Background image of page 6
Bob Tarr Design Patterns In Java The Adapter Pattern 7 Adapter Pattern Example 1 Adapter Pattern Example 1 l The classic round pegs and square pegs! l Here's the SquarePeg class: /** * The SquarePeg class. * This is the Target class. */ public class SquarePeg { public void insert(String str) { System.out.println("SquarePeg insert(): " + str); } }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Bob Tarr Design Patterns In Java The Adapter Pattern 8 Adapter Pattern Example 1 (Continued) Adapter Pattern Example 1 (Continued) l And the RoundPeg class: /** * The RoundPeg class. * This is the Adaptee class. */ public class RoundPeg { public void insertIntoHole(String msg) { System.out.println("RoundPeg insertIntoHole(): " + msg); } } l If a client only understands the SquarePeg interface for inserting pegs using the insert() method, how can it insert round pegs? A peg adapter!
Background image of page 8
Bob Tarr Design Patterns In Java The Adapter Pattern 9 Adapter Pattern Example 1 (Continued) Adapter Pattern Example 1 (Continued) l Here is the PegAdapter class: /** * The PegAdapter class. * This is the Adapter class.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

Adapter - The Adapter Pattern Design Patterns In Java Bob...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online