1 / 20

Structural Design Pattern: CLASS ADAPTER

Structural Design Pattern: CLASS ADAPTER. a.k.a. WRAPPER By Ed Kim. Context. You have discovered a legacy class that provides some or all of the functionality you need but lacks the right interface for a given domain

Télécharger la présentation

Structural Design Pattern: CLASS ADAPTER

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. Structural Design Pattern:CLASS ADAPTER a.k.a. WRAPPER By Ed Kim

  2. Context • You have discovered a legacy class that provides some or all of the functionality you need but lacks the right interface for a given domain • More generally, you would like to create a reusable class that allows classes having incompatible interfaces to work together • You may not have access to the source code to modify the interface(s) of the class(es) in question • Assuming the source code is accessible, tailoring the class’s interface to a specific application might still a bad idea (e.g., increased coupling, excessive dependencies, reduced modularity, etc.)

  3. The Wrapper Class • Present a familiar interface to the client while exploiting the services of an existing class made incompatible by its own interface • Calls made to the wrapper interface are mapped to the interface of the adapted class for actual implementation • Allows otherwise incompatible classes to work together • Existing class source code can remain intact, promoting loose coupling and increased modularity while reducing superfluous dependencies

  4. An Example

  5. The Client • Suppose your client is a North American travel iron (NATravelIron). NATravelIron

  6. The Expected Interface • Regardless of its physical location, NATravelIron expects to make use of a NEMA 5-15 socket (NEMA515Socket) in order to obtain electricity. NEMA515Socket

  7. The Incompatible Interface… • Suppose further that NATravelIron will be traveling for an indefinite period of time with its owner to Italy – a country where only sockets of type CEE 7/16 (CEE716Socket) are to be found. • How, then, will NATravelIron continue to function properly abroad? Italy CEE716Socket

  8. …The Coveted Functionality • Clearly, CEE716Socket provides the same power delivery services in Europe that NEMA515Socket would provide in North America (ignoring any voltage discrepancies for the sake of argument), but CEE716Socket simply lacks the appropriate interface that NATravelIron needs to obtain electricity and thus function properly.

  9. The Solution • Introduce a North America to Europe travel adapter (NAEUTravelAdapter) that presents the familiar NEMA515Socket interface to NATravelIron while exploiting the CEE716Socket interface to deliver power. NAEUTravelAdapter

  10. UML Model

  11. A C++ Implementation

  12. Header Files

  13. Implementation Files

  14. Tester

  15. Output

  16. Closing Remarks • Other issues, which are beyond the scope of this presentation, do exist with respect to class adapter usage – kindly refer to your nearest pattern reference for this information

More Related