Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 0 CSE3311 – Software Design Adapter Pattern
Department of Computer Science, York University Non-software analogy North American equipment electricity requirements: 110V, 60 Hz. European electricity: 220V, 50 Hz. Not practical to oBuy new equipment (computer, ipod etc) when traveling oBuild a new electricity transmission system while in Europe Solution oUse an adapter
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 2 Adapter Pattern Intent Convert the interface of a class into another interface that clients expect. Adapter lets classes with otherwise incompatible interfaces work together.
Department of Computer Science, York University Design Example Object Oriented Software Construction 13/10/ :44 AM 3 Our windowing toolkit has a drawing editor with class SHAPE with bounding box: bottom_left, top_right: POINT2D Classes such as LINE and POLYGON inherit from SHAPE An off the shelf user interface toolkit (e.g. a.NET component) has a TEXT_VIEW to display and edit text with sophisticated screen update and buffering capabilities. It’s bounding box is: origin: POINT2D width, height: REAL
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 4 Requirement We would like to re-use TEXT_VIEW as a SHAPE However, they have incompatible interfaces
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 5 Design 1 Change the TEXT_VIEW class’ features so that it’s interface is compatible with SHAPE Problem: We don’t have TEXT_VIEW’s source code – hence it is fixed in stone (e.g. a.NET assembly). Even if we have the source code, why should we have to change the interface just to make one application work. oRecall the open-closed principle
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 6 Design 2 Recall the open-closed principle Adapter Pattern
Department of Computer Science, York University Adapter – Context You want to use an existing class (TEXT_VIEW) without modifying it oAdaptee The context in which you want to use the class requires conformance to an interface that is different from that of the adaptee oTarget (SHAPE) The target interface and the adaptee interface are conceptually related
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 8 SHAPE vs. TEXT_VIEW SHAPE’s assumes a bounding box defined by its opposing corners TEXT_VIEW is defined by its origin, height and width.
Department of Computer Science, York University Adapter – Solution Define an adapter class (TEXT_SHAPE) that implements the target interface The adapter class holds a reference to the adaptee. It translates target methods to adaptee methods
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 10 Two Kinds of Adapters TEXT_SHAPE inherits SHAPE's interface and TEXT_VIEW's implementation. oInheritance/class Adapter TEXT_SHAPE2 inherits SHAPE's interface and uses TEXT_VIEW's implementation. oClient-Supplier Adapter (also mistakenly called object adapter)
Department of Computer Science, York University Client-supplier adaptor Object Oriented Software Construction 13/10/ :44 AM 11
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 12 Inheritance adapter
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 13 Participants Target (SHAPE) odefines the domain-specific interface that Client uses. Client (DrawingEditor) ocollaborates with objects conforming to the Target interface. Adaptee (TEXT_VIEW) odefines an existing interface that needs adapting. Adapter (TEXT_SHAPE) oadapts the interface of Adaptee to the Target interface.
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 14 Applicability Use the Adapter pattern when oyou want to use an existing class, and its interface does not match the one you need. oyou want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces. o(client-supplier adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. A client-supplier adapter can adapt the interface of its parent class.
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 15 Consequences Class and object adapters have different trade-offs. An inheritance adapter owon't work when we want to adapt a class and all its subclasses. olets Adapter override some of Adaptee's behavior, since Adapter is a subclass of Adaptee. ointroduces only one object, and no additional pointer indirection is needed to get to the adaptee. A client-supplier adapter olets a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once. omakes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 16 Eiffel has rich adapter mechanisms Rename Redefine Changing export status OOSC2 chapter 15 (Multiple inheritance) oUndefine oSelect
Department of Computer Science, York University Adapter real-world examples java.io.Reader is an abstract class for reading character streams [TARGET] oContains an abstract method read(char[],int,int) java.io.InputStream represents an input stream of bytes [ADAPTEE] oContains method read(byte[],int,int) java.io.InputStreamReader is an adapter from byte streams to character streams [ADAPTER] oImplements read(char[],int,int) using read(byte[],int,int)
Department of Computer Science, York University code adapter.zip Object Oriented Software Construction 13/10/ :44 AM 18