ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how...
-
Upload
brett-richardson -
Category
Documents
-
view
213 -
download
1
Transcript of ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how...
ADAPTER PATTERN
BY
Sravanthi Karumanchi
Structure PatternStructure patterns are concerned with how classes and objects are composed to form large structures.Different categories
AdapterBridgeCompositeDecoratorFaçadeFlyweightProxy
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.
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.
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.
Variations in Adapters
Class AdaptersUse multiple inheritance to compose classes
Object AdaptersObject adapters use a compositional technique to adapt one interface to another.
Object Pattern
ClientTarget
Request( )
Adaptee
SpecificRequest( )
Adapter
Request( )Adaptee->SpecificRequest( )
adaptee
Object Adapter Example
RepresentationApplication Adaptation Legacy System
Financial
amount()
Client
FinancialAdapter
amount()
Principal
ComputeValue()
Legacy Adaptee
{legacyadaptee.ComputeValue();}
Class Pattern
ClientTarget
Request( )
Adaptee
SpecificRequest( )
Adapter
Request( )Adaptee->SpecificRequest( )
implementation
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(); }
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(); }
Collaboration
Clients call operations on the Adapter instance and Adapter delegates request to Adaptee.
Clients Adapter Adapteerequest delegate
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.
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.
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
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(); } }
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.