IT. Expert System.

Design patterns

Adapter 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. A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plugins the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.

We are demonstrating use of Adapter pattern via following example in which an audio player device can play mp3 files only and wants to use an advanced audio player capable of playing vlc and mp4 files.


We've an interface MediaPlayer interface and a concrete class AudioPlayer implementing the MediaPlayer interface. AudioPlayer can play mp3 format audio files by default.

We're having another interface AdvancedMediaPlayer and concrete classes implementing the AdvancedMediaPlayer interface.These classes can play vlc and mp4 format files.

We want to make AudioPlayer to play other formats as well. To attain this, we've created an adapter class MediaAdapter which implements the MediaPlayer interface and uses AdvancedMediaPlayer objects to play the required format.

AudioPlayer uses the adapter class MediaAdapter passing it the desired audio type without knowing the actual class which can play the desired format. AdapterPatternDemo, our demo class will use AudioPlayer class to play various formats.

adapter pattern uml diagram

Step 1

Create interfaces for Media Player and Advanced Media Player.

public interface MediaPlayer {
public void play(String audioType, String fileName);

public interface AdvancedMediaPlayer {
public void playVlc(String fileName);
public void playMp4(String fileName);

Step 2

Create concrete classes implementing the AdvancedMediaPlayer interface.

public class VlcPlayer implements AdvancedMediaPlayer{
public void playVlc(String fileName) {
System.out.println("Playing vlc file. Name: "+ fileName);

public void playMp4(String fileName) {
//do nothing

public class Mp4Player implements AdvancedMediaPlayer{

public void playVlc(String fileName) {
//do nothing

public void playMp4(String fileName) {
System.out.println("Playing mp4 file. Name: "+ fileName);

Step 3

Create adapter class implementing the MediaPlayer interface.

public class MediaAdapter implements MediaPlayer {

AdvancedMediaPlayer advancedMusicPlayer;

public MediaAdapter(String audioType){
if(audioType.equalsIgnoreCase("vlc") ){
advancedMusicPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer = new Mp4Player();

public void play(String audioType, String fileName) {
}else if(audioType.equalsIgnoreCase("mp4")){

Step 4

Create concrete class implementing the MediaPlayer interface.

public class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;

public void play(String audioType, String fileName) {

//inbuilt support to play mp3 music files
System.out.println("Playing mp3 file. Name: "+ fileName);
//mediaAdapter is providing support to play other file formats
else if(audioType.equalsIgnoreCase("vlc")
|| audioType.equalsIgnoreCase("mp4")){
mediaAdapter = new MediaAdapter(audioType);, fileName);
System.out.println("Invalid media. "+
audioType + " format not supported");

Step 5

Use the AudioPlayer to play different types of audio formats.

public class AdapterPatternDemo {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();"mp3", "beyond the horizon.mp3");"mp4", "alone.mp4");"vlc", "far far away.vlc");"avi", "mind me.avi");

Step 6

Verify the output.

Playing mp3 file. Name: beyond the horizon.mp3
Playing mp4 file. Name: alone.mp4
Playing vlc file. Name: far far away.vlc
Invalid media. avi format not supported


Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)





Java Script








Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 323 / 248550694. Delta: 0.02805 с