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

Embed Size (px)

Citation preview

Page 1: 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

Chapter 7: The Adapter Pattern

Page 2: 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

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?

Page 3: 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

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?

Page 4: 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

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.

Page 5: 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

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

Page 6: 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

Object Adapter Structure

ClientTarget

<<interface>>

Adapter Adaptee

request()

request() specificrequest()

Page 7: 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

Class Adapter Structure

Client Target

Adapter

Adaptee

request()

request()

specificrequest()

Page 8: 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

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.

Page 9: 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

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.

Page 10: 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

Class Diagram for the Enumeration Adapter

Iterator<<interface>>

EnumerationIterator

EnumerationhasNext()

hasMoreElements()

next()remove()

hasNext()

next()

remove()

<<interface>>

hasElement()

Page 11: 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

How do we Implement the Adapter Pattern in this Case

• Implement an EnumerationAdapter class.• This class will implement the Iterator

interface.• Redefine the hasNext() method.• Redefine the next() method.• Redefine the remove() method?

Write an adapter that converts an Iterator to an Enumeration

Page 12: 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

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.