1 / 19

Adapter Pattern

Adapter Pattern. Jim Fawcett CSE687 – Object Oriented Design, Spring 2003 Adapted from a Presentaton by Matt Smouse and Jeff Ting CSE776 – Design Patterns, Summer 2001. Intent. Adapt an existing class rather than modify for reuse:

kaz
Download Presentation

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. Adapter Pattern Jim Fawcett CSE687 – Object Oriented Design, Spring 2003 Adapted from a Presentaton by Matt Smouse and Jeff Ting CSE776 – Design Patterns, Summer 2001

  2. Intent • Adapt an existing class rather than modify for reuse: • Convert interface of a useful class into another interface clients expect • Also known as the wrapper design pattern; it wraps existing functionality of an adaptee with an adapter’s inherited interface.

  3. Motivation • TextView example • Client is a drawing editor which wants to accommodate both shapes and text. • Adaptee is an existing TextView class which can display and edit text. • Target is a Shape class which provides the key abstraction for graphical objects. • Adapter is a TextShape class which inherits the Shape interface and adapts the TextView interface to the inherited Shape interface.

  4. Motivation: Class Adapter DrawingEditor TextView Shape GetExtent( ) BoundingBox( ) CreateManipulator( ) Line TextShape BoundingBox( ) CreateManipulator( ) BoundingBox( ) CreateManipulator( )

  5. Motivation: Object Adapter DrawingEditor Shape TextView BoundingBox( ) CreateManipulator( ) GetExtent( ) text Line TextShape BoundingBox( ) CreateManipulator( ) BoundingBox( ) CreateManipulator( )

  6. Applicability • Use an adapter when you have existing classes and need to add functionality while providing a common interface. • Use an adapter when you want a reusable class which will handle software that you did not write. • Use an object adapter when you have to provide for several different adaptee subclasses which inherit from a parent* adaptee. *Must use object adapter when adaptee is abstract.

  7. Structure: Class Adapter Client Adaptee Target SpecificRequest( ) Request( ) (implementation) Adapter Request( ) SpecificRequest( )

  8. Structure: Object Adapter Client Adaptee Target SpecificRequest( ) Request( ) Adapter Request( ) adaptee->SpecificRequest( )

  9. Participants • Target • defines the interface that the Client will use. • Client • creates and interacts with objects which conform to the target interface. • Adaptee • has an interface that needs adapting. • Adapter • adapts the interface of the adaptee to that of the target.

  10. Collaborators • Clients will create instances of the adapter and any adaptees to be used. • Clients will call operations within instances of the adapter. • The adapter will call adaptee operations on behalf of the client. • The adapter may or may not perform additional processing on any data that the adaptee methods return. • The adapter is the middleman for all operations involving the adaptee.

  11. Consequences • Class Adapter • Must commit to a concrete adaptee. This won’t work for an abstract adaptee, and it won’t work when adapting multiple adaptees. • The adapter can override some of the adaptee’s behavior; the adapter is a subclass (child) of the adaptee. • No additional pointer indirection to access adaptee. • Object Adapter • Can work with adaptee subclasses and add functionality to those subclasses. • The adapter cannot easily override adaptee behavior; only can do this by referring to an adaptee subclass.

  12. Implementation: Class Adapter • Based upon multiple inheritance • Inherit publicly from Target (the interface) and privately from Adaptee (the implementation) • Adaptor is a subtype of Target but not of Adaptee

  13. Implementation: Object Adapter • Only inherits (publicly) from Target • Adapter maintains pointer to Adaptee; Client must initialize this pointer • Will work with subclasses of Adaptee

  14. Pluggable Adapter • Special object adapter • Make the target contain abstract (pure virtual) methods. • Provide concrete operations which are common to any adapter implementation. • Provide abstract (pure virtual methods) operations which are required for unique adaptation. • Limit the number of abstract operations; it is easier to adapt a few necessary operations than to adapt many. • The client will instantiate the adapter it wishes to use; since all adapters will conform to the target interface, we can swap them in and out of the client and it won’t know the difference (beyond instantiation).

  15. Two-way Adapter • Based upon multiple inheritance • Inherits publicly from both classes (interfaces) • Two Adaptees, no Target • Used to combine functionality of two unlike classes

  16. Two-way Adapter Adaptee1 Adaptee2 Adapter

  17. Known Uses • ET++Draw • InterViews 2.6 • Pluggable adaptors in Smalltalk • FixedStack • ACE ORB (?) • Java applications (object adapters only)

  18. Related Patterns • Bridge • Separates an interface from its implementation. • Adapter works with existing object. • Decorator • Enhances another object without changing interface. • Adapter is less transparent to the client. • Proxy • Representative for another object, no interface change • Adapter changes (adapts) the object’s interface.

  19. End of Presentation

More Related