ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how...

17
ADAPTER PATTERN BY Sravanthi Karumanchi

Transcript of ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how...

Page 1: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

ADAPTER PATTERN

BY

Sravanthi Karumanchi

Page 2: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Structure PatternStructure patterns are concerned with how classes and objects are composed to form large structures.Different categories

AdapterBridgeCompositeDecoratorFaçadeFlyweightProxy

Page 3: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Scenario

Outlets and PlugsOutlets in the US require a certain kind of plug.

For example, a plug made in India for Indian outlet may not be used in USA.

To use these appliances in USA or vice-versa we may need to purchase an adapter.

Page 4: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Motivation

Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application.

We can not change the library interface, since we may not have its source code.

Even if we did have the source code, we probably should not change the library for each domain-specific application.

Page 5: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Adapter Pattern

Adapters are used to enable objects with different interfaces to communicate with each other.

Adapter Pattern tells us how to wrap up existing classes inside a new target interface, when the need arises to reuse existing class implementations but with a different interface for the clients.

They are also termed as wrappers.

Page 6: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Variations in Adapters

Class AdaptersUse multiple inheritance to compose classes

Object AdaptersObject adapters use a compositional technique to adapt one interface to another.  

Page 7: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Object Pattern

ClientTarget

Request( )

Adaptee

SpecificRequest( )

Adapter

Request( )Adaptee->SpecificRequest( )

adaptee

Page 8: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Object Adapter Example

RepresentationApplication Adaptation Legacy System

Financial

amount()

Client

FinancialAdapter

amount()

Principal

ComputeValue()

Legacy Adaptee

{legacyadaptee.ComputeValue();}

Page 9: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Class Pattern

ClientTarget

Request( )

Adaptee

SpecificRequest( )

Adapter

Request( )Adaptee->SpecificRequest( )

implementation

Page 10: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Class Adapter ExampleTarget Classclass RoundPeg { public: void virtual roundPegOperation = 0; }

Adaptee Classclass OldSquarePeg {public: void squarePegOperation(){{ do something } }

Adapter Class class PegAdapter: private OldSquarePeg, public RoundPeg {public: void virtual roundPegOperation() { add some corners; squarePegOperation(); } }

Clientvoid clientMethod() { RoundPeg* aPeg = new PegAdapter(); aPeg->roundPegOperation(); }

Page 11: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Object Adapter Example

Target Classclass RoundPeg { public: void virtual roundPegOperation = 0; }

Adaptee Classclass OldSquarePeg { public: void squarePegOperation() { do something } }

Adapter Classclass PegAdapter: public RoundPeg { private: OldSquarePeg* square;

public: PegAdapter() { square = new OldSquarePeg; } void virtual roundPegOperation() { add some corners; square->squarePegOperation(); }

Page 12: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Collaboration

Clients call operations on the Adapter instance and Adapter delegates request to Adaptee.

Clients Adapter Adapteerequest delegate

Page 13: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Adaptability

Use the adapter whenWant to use an existing class and its interface doesn’t match the one we need.

(Object Adapters only) we need to use several existing subclasses, but it’s impractical to adapt their interface by sub classing every one. An object adapter can adapt the interface of its parent class.

Page 14: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

ConsequencesClass Adapter

Adapts Adaptee to Target by committing to a concrete Adapter class.Lets Adapter override some of the Adaptee’s behavior by subclassing.Introduces only one object and no additional pointer indirection is needed to get the adaptee.

Object AdapterLets a single adapter work with a group of adaptees such as a base class and all its sub classes.The adapter can add functioanlity to all adaptees at once.Makes it harder to override Adaptee behavior as the Adapter may not know with what Adaptee it is working with.

Page 15: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

How much adapting should be done?The amount of work Adapter does depends on how similar the Target Interface is to Adapteee’s

Does the adapter provide two-way transparency?A two-way adapter supports both the Target and the Adaptee interface. It allows an adapted object (Adapter) to appear as an Adaptee object or a Target object.

Implementation Issues

Page 16: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Two-way AdapterA two-way adapter would also allow a RoundPeg be used in

place of the SquarePeg class OldSquarePeg { public: void virtual squarePegOperation() { function } }

class RoundPeg { public: void virtual roundPegOperation() { function } } class PegAdapter: public OldSquarePeg, RoundPeg { public: void virtual roundPegOperation() { add some corners; squarePegOperation(); }void virtual squarePegOperation() { add some corners; roundPegOperation(); } }

Page 17: ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.

Pluggable Adapters

A class is more reusable when you minimize the assumptions other classes must make to use it.This is achieved by building interface adaptation.