structural design pattern: class adapter
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/1.jpg)
![Page 2: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/2.jpg)
Structural Design Pattern:
CLASS ADAPTERa.k.a. WRAPPER
By Ed Kim
![Page 3: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/3.jpg)
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.)
![Page 4: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/4.jpg)
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
![Page 5: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/5.jpg)
![Page 6: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/6.jpg)
An Example
![Page 7: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/7.jpg)
The Client
• Suppose your client is a North American travel iron (NATravelIron).
NATravelIron
![Page 8: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/8.jpg)
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
![Page 9: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/9.jpg)
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
![Page 10: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/10.jpg)
…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.
![Page 11: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/11.jpg)
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
![Page 12: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/12.jpg)
UML Model
![Page 13: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/13.jpg)
![Page 14: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/14.jpg)
A C++ Implementation
![Page 15: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/15.jpg)
Header Files
![Page 16: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/16.jpg)
Implementation Files
![Page 17: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/17.jpg)
Tester
![Page 18: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/18.jpg)
Output
![Page 19: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/19.jpg)
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
![Page 20: Structural Design Pattern: CLASS ADAPTER](https://reader035.vdocument.in/reader035/viewer/2022062719/5681303b550346895d95d291/html5/thumbnails/20.jpg)