computer science 313 advanced programming topics
Download
Skip this Video
Download Presentation
Lecture 26: Adapter Pattern

Loading in 2 Seconds...

play fullscreen
1 / 18

Lecture 26: Adapter Pattern - PowerPoint PPT Presentation


  • 88 Views
  • Uploaded on

Computer Science 313 – Advanced Programming Topics. Lecture 26: Adapter Pattern. Real-World Problems. Have one thing, but need something different Try & make it work despite not being correct But this often leads to problems. Outlets Around the World.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 26: Adapter Pattern' - amish


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
real world problems
Real-World Problems
  • Have one thing, but need something different
    • Try & make it work despite not being correct
    • But this often leads to problems
outlets around the world
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
play with electricity for fun
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
play with electricity for fun1
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
play with electricity for fun2
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
adapter pattern workings
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
adapter pattern intent
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!
adapter pattern example
Adapter Pattern Example
  • Already wrote class sorting an array

public interface Comparable<T> {public intcompareTo(T other);

}

public class QuickSort {static void <T extends Comparable<T>> sort(T[] array) { // Sorts this array}

}

points on a plane
Points on a Plane
  • Must sort, but Dimensions not Comparable
    • Rewrite QuickSortto use Dimensions
    • Dimensionclosed 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
object adapter
Object Adapter

class ComparableDimensionimplements Comparable<ComparableDimension> {private Dimension d; // Object being adaptedpublic ComparableDimension(Dimension newD) {d = newD;}public intcompareTo(ComparableDimension o) { Integer myArea = d.height * d.width; Integer oArea = o.d.height * o.d.width; return myArea.compareTo(oArea);}

}

class adapter
Class Adapter

class ComparableDimensionextends Dimensionimplements Comparable<ComparableDimension> {public ComparableDimension(int h, int w) {super(h, w);}public intcompareTo(ComparableDimension o) { Integer myArea = d.height * d.width; Integer oArea = o.d.height * o.d.width; return myArea.compareTo(oArea);}

}

how example works
How Example Works
  • ImplementingComparableDimension either way
    • Is-a or Has-a Dimensionto compare
    • Comparableinterface is implemented
    • Can be used by QuickSortclass
class adapter uml diagram
Class Adapter UML Diagram
  • Adapterextends Target & is-aAdaptee
    • Not very easy in Java
  • Client, Target & Adapteealready exist
    • Only Adapter created or edited to do this
class adapter uml diagram1
Class Adapter UML Diagram
  • Adapterextends Target & is-aAdaptee
    • Not very easy in Java (or most sane languages)
  • Client, Target & Adapteealready exist
    • Only Adapter created or edited to do this
object adapter uml diagram
Object Adapter UML Diagram
  • Adapterextends Target & has-aAdaptee
    • No multiple inheritance, so legal in Java
  • Client, Target & Adapteealready exist
    • Only Adapter created or edited to do this
object adapter uml diagram1
Object Adapter UML Diagram
  • Adapterextends Target & has-aAdaptee
    • No multiple inheritance, so legal in Java
  • Client, Target & Adapteealready exist
    • Only Adapter created or edited to do this
for next class
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?
ad