ADAPTER PATTERN
BY
Sravanthi Karumanchi
Structure Pattern
Structure patterns are concerned with how
classes and objects are composed to form
large structures.
Dierent categories
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
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.
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-speci$c application.
Adapter Pattern
Adapters are used to enable objects with
dierent 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
dierent interface for the clients.
They are also termed as wrappers.
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.
Object Pattern
Client
Target
Request( )
Adaptee
SpecificRequest( )
Adapter
Request( )
Adaptee->SpecificRequest( )
adaptee
Object Adapter Example
Representation
Application Adaptation Legacy System
Financial
amount()
Client
FinancialAdapter
amount()
Principal
ComputeValue()
Legacy Adaptee
{legacyadaptee.ComputeValue();}
Class Pattern
Client
Target
Request( )
Adaptee
SpecificRequest( )
Adapter
Request( )
Adaptee->SpecificRequest( )
implementation
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(); }
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(); }
Collaboration
Clients call operations on the Adapter instance and
Adapter delegates request to Adaptee.
Clients Adapter Adaptee
request
delegate
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.
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.
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 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(); } }
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.