structural design pattern: class adapter

20

Upload: alea-wallace

Post on 31-Dec-2015

35 views

Category:

Documents


1 download

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 Presentation

TRANSCRIPT

Page 1: Structural Design Pattern: CLASS ADAPTER
Page 2: Structural Design Pattern: CLASS ADAPTER

Structural Design Pattern:

CLASS ADAPTERa.k.a. WRAPPER

By Ed Kim

Page 3: Structural Design Pattern: CLASS ADAPTER

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

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
Page 6: Structural Design Pattern: CLASS ADAPTER

An Example

Page 7: Structural Design Pattern: CLASS ADAPTER

The Client

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

NATravelIron

Page 8: Structural Design Pattern: CLASS ADAPTER

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

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

…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

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

UML Model

Page 13: Structural Design Pattern: CLASS ADAPTER
Page 14: Structural Design Pattern: CLASS ADAPTER

A C++ Implementation

Page 15: Structural Design Pattern: CLASS ADAPTER

Header Files

Page 16: Structural Design Pattern: CLASS ADAPTER

Implementation Files

Page 17: Structural Design Pattern: CLASS ADAPTER

Tester

Page 18: Structural Design Pattern: CLASS ADAPTER

Output

Page 19: Structural Design Pattern: CLASS ADAPTER

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