Adapter Design Pattern: CECS 470 Fall 2017 Mimi Opkins
Adapter Design Pattern: CECS 470 Fall 2017 Mimi Opkins
CECS 470
Fall 2017
Mimi Opkins
Adapter Pattern
• Adapter is a structural design pattern, which allows
incompatible objects to cooperate.
• Adapter acts as a wrapper between two objects, It
catches calls for one object and transforms them to
format and interface recognizable by the second
object.
• In other words, how we put a square peg in a round
hole.
Definition & Applicability
• Adapters are used to enable objects with different interfaces to communicate with each
other.
• The Adapter pattern is used to convert the programming interface of one class into that of
another.
• We use adapters whenever we want unrelated classes to work together in a single program.
• Adapters come in two flavors,
– object adapters and
– class adapters.
• The concept of an adapter is thus pretty simple; we write a class that has the desired
interface and then make it communicate with the class that has a different interface.
• Adapters in Java can be implemented in two ways:
– by inheritance, and
– by object composition.
Object Adapters
/**
* The RoundPeg class.
* This is the Adaptee class.
*/
public class RoundPeg {
public void insertIntoHole(String msg) {
System.out.println("RoundPeg insertIntoHole(): " + msg);}
}
Example 1
• If a client only understands the SquarePeg interface
for inserting pegs using the insert() method, how can
it insert round pegs, which are pegs, but that are
inserted differently, using the insertIntoHole()
method?
Example 2
Solution:
Execution trace:
SquarePeg insert(): Inserting square peg...
RoundPeg insertIntoHole(): Inserting round peg...
Class Adapters
• Class adapters use multiple interfaces to achieve their goals.
• As in the object adapter, the class adapter inherits the
interface of the client's target. However, it also inherits the
interface of the adaptee as well.
• Since Java does not support true multiple inheritance, this
means that one of the interfaces must be inherited from a
Java Interface type.
• Both of the target or adaptee interfaces could be Java
Interfaces.
• The request to the target is simply rerouted to the specific
request that was inherited from the adaptee interface.
Consequences of the Adapter Pattern
An object adapter
• Let’s a single Adapter work with many Adaptees - that is, the Adaptee itself
and all of its subclasses (if any). The Adapter can also add functionality to all
Adaptees at once.
• Makes it harder to override Adaptee behavior. It will require subclassing
Adaptee and making Adapter refer to the subclass rather than the Adaptee
itself.
.
• Adapter pattern works as a bridge between two incompatible interfaces. This type
of design pattern comes under structural pattern as this pattern combines the
capability of two independent interfaces.
• We want to make MP3 to play other formats as well. To attain this, we have
created an adapter class FormatAdapter which implements the MediaPlayer
interface and uses MediaPackage objects to play the required format.
• MP3 uses the adapter class FormatAdapter passing it the desired audio type
without knowing the actual class which can play the desired format. Main,
our demo class will use MP3 class to play various formats.
public interface MediaPlayer { void play(String filename);}
void play(String filename);
Step 1
MediaPlayer.java
MediaPackage.java
VlcPlayer.java
Mp4Player.java
@Override
public void playFile(String fileName) {
System.out.println("Playing mp4 file. Name: "+ fileName);
}
}
Step 4
Create adapter class implementing the MediaPlayer interface.
• When you need to use an existing class and its interface is not
the one you need, use an adapter: allows collaboration
between classes with incompatible interfaces.
• An adapter changes an interface into one a client expects.
• Implementing an adapter may require little work or a great deal
of work depending on the size and complexity of the target
interface.
• There are two forms of adapter patterns: object and class
adapters.
• Class adapters require multiple inheritance.
Credits
• Java Design Patterns - DEPARTMENT OF COMPUTER SCIENCE
AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY -
Emil Vassev, Joey Paquet : 2006-2012
• Tutorials Point Design Patterns – Adapter Pattern -
https://www.tutorialspoint.com/design_pattern/adapter_
pattern.htm
• Implement the Adapter Design Pattern in Java
https://medium.com/@ssaurel/implement-the-adapter-d
esign-pattern-in-java-f9adb6a8828f