Download presentation
Presentation is loading. Please wait.
Published byPhilip Hodges Modified over 9 years ago
1
Computer Science 313 – Advanced Programming Topics
2
Real-World Problems Have one thing, but need something different Try & make it work despite not being correct But this often leads to problems
3
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
4
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
5
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
6
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
7
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
8
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 D ECORATOR PATTERN Does not need to expose full functionality Have not discussed what plug attached to!
9
Adapter Pattern Example Already wrote class sorting an array public interface Comparable { public int compareTo(T other); } public class QuickSort { static void > sort(T[] array) { // Sorts this array } }
10
Points on a Plane Must sort, but Dimension s not Comparable Rewrite QuickSort to use Dimension s 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 A DAPTER PATTERN Hide object, via Object adapter using composition Class adapter uses inheritance, but reuse limited
11
Object Adapter class ComparableDimension implements Comparable { 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); } }
12
Class Adapter class ComparableDimension extends Dimension implements Comparable { 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); } }
13
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
14
Class Adapter UML Diagram is-a Adapter extends Target & is-a Adaptee Not very easy in Java Client, Target & Adaptee already exist Only Adapter created or edited to do this
15
Class Adapter UML Diagram is-a Adapter extends Target & is-a Adaptee Not very easy in Java Client, Target & Adaptee already exist Only Adapter created or edited to do this
16
Object Adapter UML Diagram has-a 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
17
Object Adapter UML Diagram has-a 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
18
For Next Class Lab #6 available on Angel Assignment due before we go on break 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.