Lecture 26: Adapter Pattern
description
Transcript of Lecture 26: Adapter Pattern
![Page 1: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/1.jpg)
LECTURE 26:ADAPTER PATTERN
Computer Science 313 – Advanced Programming Topics
![Page 2: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/2.jpg)
Real-World Problems
Have one thing, but need something different Try & make it work despite not being
correct But this often leads to problems
![Page 3: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/3.jpg)
Outlets Around the World
2 standards for electric transmission Japan & the Americas use 110 volt 220 volts used by Europe, (rest of) Asia,
& Africa Variety of incompatible plugs defined
by each
![Page 4: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/4.jpg)
Play With Electricity for Fun! Do not want new appliance for each
country First thought: jam single plug into
wall Plug interface only difference between
them
![Page 5: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/5.jpg)
Play With Electricity for Fun! Do not want new appliance for each
country First thought: jam single plug into
wall Plug interface only difference between
them
![Page 6: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/6.jpg)
Play With Electricity for Fun! Do not want new appliance for each
country First thought: jam single plug into
wall Plug interface only difference between
them Generally speaking, this is not a good
idea Use plug adapter as an alternate
approach Plug then good to use in existing outlets
now
![Page 7: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/7.jpg)
Adapter Pattern Workings
Like plug adapters, but used with existing code Starts with some existing plug (instance) Need it to match a preexisting outlet
(interface) Do not want big changes; functionality
stays same Change what is exposed by wrapping plug
![Page 8: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/8.jpg)
Adapter Pattern Intent
Makes existing class use client's interface Otherwise compatible classes now work
together Work is invisible to client code
Goal of pattern is client code is not modified
No changes to functionality with this pattern If we want real changes use DECORATOR
PATTERN Does not need to expose full
functionality Have not discussed what plug attached
to!
![Page 9: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/9.jpg)
Adapter Pattern Example
Already wrote class sorting an arraypublic interface Comparable<T> {public int compareTo(T other);
}
public class QuickSort {static void <T extends Comparable<T>> sort(T[] array) { // Sorts this array}
}
![Page 10: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/10.jpg)
Points on a Plane
Must sort, but Dimensions not Comparable Rewrite QuickSort to use Dimensions Dimension closed to modification &
inheritance Do not want to rerun tests on Dimension,
anyway Adapter does this without violating laziness
principle Two ways to do this with ADAPTER
PATTERN Hide object, via Object adapter using
composition Class adapter uses inheritance, but reuse
limited
![Page 11: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/11.jpg)
Object Adapter
class ComparableDimension implements Comparable<ComparableDimension> {private Dimension d; // Object being adapted
public ComparableDimension(Dimension newD) { d = newD;}
public int compareTo(ComparableDimension o) { Integer myArea = d.height * d.width; Integer oArea = o.d.height * o.d.width; return myArea.compareTo(oArea);}
}
![Page 12: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/12.jpg)
Class Adapter
class ComparableDimension extends Dimensionimplements Comparable<ComparableDimension> {
public ComparableDimension(int h, int w) { super(h, w);}
public int compareTo(ComparableDimension o) { Integer myArea = d.height * d.width; Integer oArea = o.d.height * o.d.width; return myArea.compareTo(oArea);}
}
![Page 13: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/13.jpg)
How Example Works
Implementing ComparableDimension either way Is-a or Has-a Dimension to compare Comparable interface is implemented Can be used by QuickSort class
![Page 14: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/14.jpg)
Class Adapter UML Diagram
Adapter extends Target & is-a Adaptee Not very easy in Java
Client, Target & Adaptee already exist Only Adapter created or edited to do
this
![Page 15: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/15.jpg)
Class Adapter UML Diagram
Adapter extends Target & is-a Adaptee Not very easy in Java (or most sane
languages) Client, Target & Adaptee already
exist Only Adapter created or edited to do
this
![Page 16: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/16.jpg)
Object Adapter UML Diagram Adapter extends Target & has-a Adaptee No multiple inheritance, so legal in Java
Client, Target & Adaptee already exist Only Adapter created or edited to do this
![Page 17: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/17.jpg)
Object Adapter UML Diagram Adapter extends Target & has-a Adaptee No multiple inheritance, so legal in Java
Client, Target & Adaptee already exist Only Adapter created or edited to do this
![Page 18: Lecture 26: Adapter Pattern](https://reader036.fdocuments.us/reader036/viewer/2022062520/56815e76550346895dccf8d0/html5/thumbnails/18.jpg)
For Next Class
Lab #5 due on Friday before next lab starts Get it working while you can still earn
credit Will discuss Façade pattern on
Wednesday How does this differ from Adapter? What is important when discussing
patterns? What is the most “Hollywood” of
patterns?