ADAPTER PATTERN
BY
IBAD –UR-REHMAN
Structure Pattern
Structural design patterns are concerned with
how classes and objects can be composed, to form
larger structures.
The structural design patterns simplifies the
structure by identifying the relationships.
These patterns focus on, how the classes inherit
from each other and how they are composed from
other classes.
Types of Structure Pattern
Different categories
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
Adapter Pattern
Allow an application to use external
functionality in a retarget able manner
Adapters are used to enable objects with
different interfaces to communicate with
each other.
They are also termed as wrappers.
Design Summary
Write the application against the abstract
version of external class, introduce the sub-
class that aggregate the external class
UML
Client
Target
Request( )
Adaptee
SpecificRequest( )
Adapter
Request( )
Adaptee->SpecificRequest( )
adaptee
Real-Life Application
Create interfaces for MediaPlayer and MediaPackage.
MediaPlayer.java
public interface MediaPlayer {
public void play(String fileName);
}
MediaPackage.java
public interface MediaPackage {
public void playFile(String fileName);
}
Real-Life Application (cont.)
Create concrete classes implementing the MediaPlayer interface
public class MP3 implements MediaPlayer {
@Override
public void play(String filename) {
System.out.println("Playing MP3 File " + filename);
}
Real-Life Application (cont.)
Create concrete classes implementing the MediaPackage
interface.
VlcPlayer.java
public class VlcPlayer implements MediaPackage{
@Override
public void playFile(String fileName) {
System.out.println("Playing vlc file. Name: "+ fileName);
}
}
Real-Life Application (cont.)
Mp4Player.java
public class Mp4Player implements MediaPackage {
@Override
public void playFile(String fileName) {
System.out.println("Playing mp4 file. Name: "+
fileName);
}
}
Real-Life Application (cont.)
Create adapter class implementing the MediaPlayer
interface.
public class FormatAdapter implements MediaPlayer {
private MediaPackage media;
public FormatAdapter(MediaPackage m) {
media = m;
}
@Override
public void play(String filename) {
System.out.print("Using Adapter --> ");
media.playFile(filename);
}
Real-Life Application (cont.)
public class Main { public static void main(String[]
args) {
MediaPlayer player = new MP3();
player.play("file.mp3");
player = new FormatAdapter(new MP4());
player.play("file.mp4");
player = new FormatAdapter(new VLC());
player.play("file.avi");
}
}
Purpose & Usage
An adapter class provides the default
implementation of all methods in an event
listener interface. Adapter classes are very
useful when you want to process only few
of the events that are handled by a particular
event listener interface. You can define a
new class by extending one of the adapter
classes and implement only those events
relevant to you.
Why in Structural Pattern.
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.
This pattern involves a single class which is
responsible to join functionalities of
independent or incompatible interfaces

Adapter pattern

  • 1.
  • 2.
    Structure Pattern Structural designpatterns are concerned with how classes and objects can be composed, to form larger structures. The structural design patterns simplifies the structure by identifying the relationships. These patterns focus on, how the classes inherit from each other and how they are composed from other classes.
  • 3.
    Types of StructurePattern Different categories Adapter Bridge Composite Decorator Façade Flyweight Proxy
  • 4.
    Adapter Pattern Allow anapplication to use external functionality in a retarget able manner Adapters are used to enable objects with different interfaces to communicate with each other. They are also termed as wrappers.
  • 5.
    Design Summary Write theapplication against the abstract version of external class, introduce the sub- class that aggregate the external class
  • 6.
  • 7.
    Real-Life Application Create interfacesfor MediaPlayer and MediaPackage. MediaPlayer.java public interface MediaPlayer { public void play(String fileName); } MediaPackage.java public interface MediaPackage { public void playFile(String fileName); }
  • 8.
    Real-Life Application (cont.) Createconcrete classes implementing the MediaPlayer interface public class MP3 implements MediaPlayer { @Override public void play(String filename) { System.out.println("Playing MP3 File " + filename); }
  • 9.
    Real-Life Application (cont.) Createconcrete classes implementing the MediaPackage interface. VlcPlayer.java public class VlcPlayer implements MediaPackage{ @Override public void playFile(String fileName) { System.out.println("Playing vlc file. Name: "+ fileName); } }
  • 10.
    Real-Life Application (cont.) Mp4Player.java publicclass Mp4Player implements MediaPackage { @Override public void playFile(String fileName) { System.out.println("Playing mp4 file. Name: "+ fileName); } }
  • 11.
    Real-Life Application (cont.) Createadapter class implementing the MediaPlayer interface. public class FormatAdapter implements MediaPlayer { private MediaPackage media; public FormatAdapter(MediaPackage m) { media = m; } @Override public void play(String filename) { System.out.print("Using Adapter --> "); media.playFile(filename); }
  • 12.
    Real-Life Application (cont.) publicclass Main { public static void main(String[] args) { MediaPlayer player = new MP3(); player.play("file.mp3"); player = new FormatAdapter(new MP4()); player.play("file.mp4"); player = new FormatAdapter(new VLC()); player.play("file.avi"); } }
  • 14.
    Purpose & Usage Anadapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.
  • 15.
    Why in StructuralPattern. 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. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces