Unformatted text preview:

AdapterKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/6448 — Lecture 23 — 11/13/2007© University of Colorado, 20071Friday, November 16, 2007Lecture Goals• Cover Material from Chapter 7 of the Design Patterns Textbook• Adapter Pattern• Facade Pattern2Friday, November 16, 2007Adapters in the Real World• Our next pattern provides techniques for converting an interface that is not compatible with an existing system into a different interface that is• Real World Example: AC Power Adapters• Electronic products made for the USA cannot be used directly with electrical outlets found in most other parts of the world• US 3-prong (grounded) plugs are not compatible with European wall outlets• To use, you need either• an AC power adapter, if the US product has a “universal” power supply, or• an AC power convertor/adapter, if it doesn’t• By example, OO adapters may simply provide adaptation services from one interface to another, or may require more smarts to convert information from one interface before passing it to the second interface3Friday, November 16, 2007OO Adapters (I)• Pre-Condition: You are maintaining an existing system that makes use of a third-party class library from vendor A• Stimulus: Vendor A goes belly up and corporate policy does not allow you to make use of an unsupported class library.• Response: Vendor B provides a similar class library but its interface is completely different from the interface provided by vendor A• Assumptions: You don’t want to change your code, and you can’t change vendor B’s code.• Solution?: Write new code that adapts vendor B’s interface to the interface expected by your original code4Friday, November 16, 2007OO Adapters (II)ExistingSystemVendorBClassLibraryInterface MismatchNeed AdapterAdapterCreate AdapterAnd then...5Friday, November 16, 2007VendorBClassLibraryOO Adapters (III)AdapterExistingSystem...plug it inBenefit: Existing system and new vendor library do not change, new code is isolated within the adapter.6Friday, November 16, 2007Simple Example: A turkey hiding among ducks! (I)• If it walks like a duck and quacks like a duck, then it must be a duck!7Friday, November 16, 2007Simple Example: A turkey hiding among ducks! (II)•If it walks like a duck and quacks like a duck, then it must might be a duck turkey wrapped with a duck adapter… (!)• Recall the Duck simulator from chapter 1?public interface Duck {1 public void quack();2 public void fly();3}45public class MallardDuck implements Duck {67 public void quack() {8 System.out.println("Quack");9 }10 11 public void fly() {12 System.out.println("I'm flying");13 }14}15168Friday, November 16, 2007Simple Example: A turkey hiding among ducks! (III)• An interloper wants to invade the simulatorpublic interface Turkey {1 public void gobble();2 public void fly();3}45public class WildTurkey implements Turkey {67 public void gobble() {8 System.out.println("Gobble Gobble");9 }10 11 public void fly() {12 System.out.println("I'm flying a short distance");13 }14 15}16179Friday, November 16, 2007Simple Example: A turkey hiding among ducks! (IV)• Write an adapter, that makes a turkey look like a duckpublic class TurkeyAdapter implements Duck {12 private Turkey turkey;3 4 public TurkeyAdapter(Turkey turkey) {5 this.turkey = turkey;6 }7 8 public void quack() {9 turkey.gobble();10 }11 12 public void fly() {13 for (int i = 0; i < 5; i++) {14 turkey.fly();15 }16 }17 18}19201. Adapter implements target interface (Duck).2. Adaptee (turkey) is passed via constructor and stored internally3. Calls by client code are delegated to the appropriate methods in the adaptee4. Adapter is full-fledged class, could contain additional vars and methods to get its job doneDemonstration10Friday, November 16, 2007Adapter Pattern: Definition• The Adapter pattern converts the interface of a class into another interface that clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces• The client makes a request on the adapter by invoking a method from the target interface on it• The adapter translates that request into one or more calls on the adaptee using the adaptee interface• The client receives the results of the call and never knows there is an adapter doing the translation11Friday, November 16, 2007Adapter Pattern: Structure (I)Object AdapterClientrequest()Target«interface»request()AdapterspecificRequest()Adapteeadaptee.specificRequest()1. Client codes to aninterface, not animplementation. Allowscreation of multiple adapterclasses, if needed.2. Adapter makes use ofcomposition to access thebehavior of Adaptee. We canpass any subclass of Adapteeto the Adapter, if needed.12Friday, November 16, 2007Adaptee Pattern: Structure (II)Class AdapterClientrequest()Targetrequest()AdapterspecificRequest()Adapteeadaptee.specificRequest()1. Requires use of multipleinheritance, but now adapterdoes not need to re-implementtarget and/or adaptee behavior.It simply overrides or inheritsthat behavior instead.Trade-Offs?Demonstration13Friday, November 16, 2007Real World Adapters• Before Java’s new collection classes, iteration over a collection occurred via java.util.Enumeration• hasMoreElements() : boolean• nextElement() : Object• With the collection classes, iteration was moved to a new interface: java.util.Iterator• hasNext(): boolean• next(): Object• remove(): void• There’s a lot of code out there that makes use of the Enumeration interface• New code can still make use of that code by creating an adapter that converts from the Enumeration interface to the Iteration interface• Demonstration14Friday, November 16, 2007Difference between Adapter and Decorator• Adapter and Decorator’s seem similar: how so?• Answers• They both wrap objects at run-time• They both delegate requests to their wrapped objects• How are they different?• Answers• Adapter converts one interface into another while maintaining functionality • Decorator leaves the interface alone but adds new functionality• Decorators are designed to be “stacked”; that’s less likely to occur with adapters15Friday, November 16, 2007Yet Another Adapter: Facade Pattern• There is another way in which an adapter can be used between a client and an adaptee: to simplify the interface of the adaptee(s)• Imagine a library of classes with a


View Full Document

CU-Boulder CSCI 6448 - Adapter

Documents in this Course
Struts

Struts

12 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Adapter
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Adapter and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Adapter 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?