Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures."— Presentation transcript:

1 ADAPTER PATTERN BY Sravanthi Karumanchi

2 Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures. Different categories Adapter Bridge Composite Decorator Façade Flyweight Proxy

3 Scenario Outlets and Plugs Outlets 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.

4 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.

5 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.

6 Variations in Adapters Class Adapters Use multiple inheritance to compose classes Object Adapters Object adapters use a compositional technique to adapt one interface to another.

7 Object Pattern Client Target Request( ) Adaptee SpecificRequest ( ) Adapter Request( ) Adaptee->SpecificRequest( ) adaptee

8 Object Adapter Example Representation ApplicationAdaptationLegacy System Financial amount() Client FinancialAdapter amount() Principal ComputeValue() Legacy Adaptee {legacyadaptee.ComputeValue();}

9 Class Pattern Client Target Request( ) Adaptee SpecificRequest ( ) Adapter Request( ) Adaptee->SpecificRequest( ) implementation

10 Class Adapter Example Target Class class RoundPeg { public: void virtual roundPegOperation = 0; } Adaptee Class class OldSquarePeg { public: void squarePegOperation() {{ do something } } Adapter Class class PegAdapter: private OldSquarePeg, public RoundPeg { public: void virtual roundPegOperation() { add some corners; squarePegOperation(); } } Client void clientMethod() { RoundPeg* aPeg = new PegAdapter(); aPeg->roundPegOperation(); }

11 Object Adapter Example Target Class class RoundPeg { public: void virtual roundPegOperation = 0; } Adaptee Class class OldSquarePeg { public: void squarePegOperation() { do something } } Adapter Class class PegAdapter: public RoundPeg { private: OldSquarePeg* square; public: PegAdapter() { square = new OldSquarePeg; } void virtual roundPegOperation() { add some corners; square- >squarePegOperation(); }

12 Collaboration Clients call operations on the Adapter instance and Adapter delegates request to Adaptee. ClientsAdapterAdaptee request delegate

13 Adaptability Use the adapter when Want 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.

14 Consequences Class 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 Adapter Lets 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.

15 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

16 Two-way Adapter A 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(); } }

17 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.


Download ppt "ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures."

Similar presentations


Ads by Google