SlideServe
Menu
Préférences
© SlideServe · v2026

Utilizing the Adapter Pattern: Bridging Interface Gaps in Java

nyssa-casey avatar
12 diapositives · 221 Views · Nov 13, 2014

Learn how to implement the Adapter Pattern in Java to bridge interface differences between existing and new classes without modifying source code. Examples include adapting Duck to Turkey and converting Enumeration to Iterator.

Utilizing the Adapter Pattern: Bridging Interface Gaps in Java - slide 1
01 / 12Utilizing the Adapter Pattern: Bridging Interface Gaps in Java
1 / 17

Télécharger la présentation

Utilizing the Adapter Pattern: Bridging Interface Gaps in Java
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.