1 / 17

Chapter Seven The Adapter Pattern

Chapter Seven The Adapter Pattern. Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University. Outline. Introducing the Adapter Pattern Learning the Adapter Pattern Field Notes: The Adapter Pattern Summary.

tparker
Download Presentation

Chapter Seven 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 SevenThe Adapter Pattern Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

  2. Outline • Introducing the Adapter Pattern • Learning the Adapter Pattern • Field Notes: The Adapter Pattern • Summary The Adapter Pattern

  3. Introducing the Adapter Pattern • Intent by GoF • Convert the interface of a class into another interface that the clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces. • Also known as Wrapper The Adapter Pattern

  4. Outline • Introducing the Adapter Pattern • Learning the Adapter Pattern • Field Notes: The Adapter Pattern • Summary The Adapter Pattern

  5. Learning the Adapter Pattern • Consider the following requirements • Create classes for points, lines, and squares that have the behavior “display.” • The client objects should not have to know whether they actually have a point, a line, or a square. They just want to know that they have one of these shapes. The Adapter Pattern

  6. Learning the Adapter Pattern • Encompass these specific shapes in a higher-level concept that I will call a “displayable shape.” The Adapter Pattern

  7. The Adapter Pattern • Make use of polymorphism • Specify the particular behavior that Shapes are going to provide. • Set a Shape’s location. • Get a Shape’s location. • Display a Shape. • Fill a Shape. • Set the color of a Shape. • Undisplay a Shape. The Adapter Pattern

  8. The Adapter Pattern The Adapter Pattern

  9. The Adapter Pattern • XXCircle with the following methods • displayIt • fillIt • undisplayIt • Cannot use XXCircle directly • I have different names and parameter lists • I cannot derive it • What can I do? • I have what I want almost within reach, but I cannot use it and I do not want to rewrite it. The Adapter Pattern

  10. The Adapter Pattern • Rather than change it, I adapt it. • Class Circle derives from Shape • Circle contains XXCircle • Circle passes requests made to the Circle object on through to the XXCircle object. The Adapter Pattern

  11. Implementation The Adapter Pattern

  12. Outline • Introducing the Adapter Pattern • Learning the Adapter Pattern • Field Notes: The Adapter Pattern • Summary The Adapter Pattern

  13. Field Notes: The Adapter Pattern • You can do more than wrapping. • Functions in the existing class can be adapted • Functions not present can be implemented in the wrapping object • Free you from worrying about existing interfaces • Variations • Object adapter pattern • Rely on one object containing another • Class adapter pattern • Multiple inheritance The Adapter Pattern

  14. Comparing the Adapterwith the Facade • Both are wrappers • Subtle differences The Adapter Pattern

  15. Outline • Introducing the Adapter Pattern • Learning the Adapter Pattern • Field Notes: The Adapter Pattern • Summary The Adapter Pattern

  16. The Adapter Pattern • A Facade simplifies an interface while an Adapter converts the interface into a preexisting interface. • A very useful pattern that converts the interface of a class (or classes) into another interface, which we need the class to have. • Be implemented by creating a new class with the desired interface and then wrapping the original class methods to effectively contain the adapted object. The Adapter Pattern

  17. The End

More Related