Adapter Presentation
Adapter Presentation
Adapter Pattern
●
Intent
●
A structural design pattern that convert the interface of a
class into another interface clients expect. Adapter lets
classes work together that couldn't otherwise because of
incompatible interfaces
●
Wrap an existing class with a new interface.
Adapter Pattern
●
Problem
Let we have a media player which can play audio for some
reasons we want our media player can also play MP4 and
VLC files
●
Solution
●
We may write new code
●
We may use existing code that developed by others (3rd
party) or we developed before
Adapter Pattern
●
There is a catch
●
Our previous code is not compatible with our current project
●
Others code is also not compatible with our project
●
Solution?
●
We can change our/others code
●
We can use an adapter which can
convert the incompatible code
into our desired format
Adapter Pattern
●
Applicability
●
Use the Adapter class when you want to use some existing
class, but its interface isn’t compatible with the rest of your
code.
●
Use the adapter class for creating a reusable class that
cooperates with unrelated or unforeseen classes, that is,
classes that don't necessarily have compatible interfaces
●
Use the adapter pattern when you need to use several
existing subclasses, but it's impractical to adapt their interface
by subclassing every one. An object adapter can adapt the
interface of its parent class
Adapter Pattern
(How to do?)
Multiple Inheritance
Adapter Pattern
(How to do?)
Composition
Adapter Pattern
●
Participants
●
Target
●
defines the domain-specific interface that Client uses.
●
Client
●
collaborates with objects conforming to the Target interface.
●
Adaptee
●
defines an existing interface that needs adapting.
• Adapter
●
adapts the interface of Adaptee to the Target interface.
Adapter Pattern
(Implementation)
Adapter Pattern
(Implementation: Client )
Adapter Pattern
(Implementation: Target Class)
Adapter Pattern
(Implementation: Adapter Class)
Adapter Pattern
(Implementation: Adaptee Class)
THANK YOU