1 / 12

Chapter 7: The Adapter Pattern

Chapter 7: The Adapter Pattern. Object Oriented Adapters. Suppose that you have existing software. You have outsourced some of your work and there is a new library that needs to be added to the existing software.

nyssa-casey
Download Presentation

Chapter 7: The Adapter Pattern

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7: The Adapter Pattern

  2. Object Oriented Adapters • Suppose that you have existing software. • You have outsourced some of your work and there is a new library that needs to be added to the existing software. • Problem: The interfaces of the new class library are different to that of the existing software. • The new class library cannot be changed as you do not have the source code. • Should you change the existing code?

  3. Duck and Turkey Example • Suppose that we have an interface for the type Duck and the type Turkey. • The Duck interface has the methods: • quack() • fly() • The Turkey interface has the methods • gobble() • fly() • How would you use Turkey objects as Duck objects?

  4. Using an Adapter • We do not want to change the Duck interface or the Turkey interface. • So we write an adapter that can convert from one to the other. • To use a Turkey object as a Duck object you need to write a Turkey adapter. • The adapter must implement the Duck interface. • The adapter must redefine the quack() method and the fly() method.

  5. The Adapter Pattern • Converts the interface of one class to another thus enabling classes with incompatible interfaces to work together. • There are two types of adapters: • Object adapters, e.g. the duck-turkey example • Class adapters • Write an adapter that converts a Duck to a Turkey

  6. Object Adapter Structure

  7. Class Adapter Structure

  8. Example of Using an Adapter in Java • Collection classes in Java, e.g. Vector, Stack implement a method elements() which returns a the elements of the structure. • Older versions of Java return this as an Enumeration. • Later versions return the elements as an Iterator. • Both Enumeration and Iterator are interfaces. • An adapter will be needed to convert from one interface type to another.

  9. Enumeration vs. Iterator • Enumeration interface methods: • hasNextElement() • nextElement() • Iterator interface methods: • hasNext() • next() • remove() • Draw a class diagram and write the code to adapt and Enumeration to an Iterator.

  10. Class Diagram for the Enumeration Adapter

  11. How do we Implement the Adapter Pattern in this Case • Implement an EnumerationAdapter class. • This class will implement the Iteratorinterface. • Redefine the hasNext() method. • Redefine the next() method. • Redefine the remove() method? Write an adapter that converts an Iterator to an Enumeration

  12. In Summary… • An adapter is used when one needs to use and existing class that does not have the interface that is needed. • The adapter pattern changes the interface to one the client expects. • Design Principle: Principle of least knowledge: Only talk to your friends.

More Related