<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Adapter_Design_Pattern</id>
	<title>Adapter Design Pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Adapter_Design_Pattern"/>
	<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Adapter_Design_Pattern&amp;action=history"/>
	<updated>2026-04-22T13:35:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>http://rasimsen.com/index.php?title=Adapter_Design_Pattern&amp;diff=572&amp;oldid=prev</id>
		<title>Rasimsen: Created page with &quot;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 tw...&quot;</title>
		<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Adapter_Design_Pattern&amp;diff=572&amp;oldid=prev"/>
		<updated>2018-08-12T21:28:12Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;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 tw...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;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.&lt;br /&gt;
&lt;br /&gt;
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 plugin the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=Implementation=&lt;br /&gt;
We have a MediaPlayer interface and a concrete class AudioPlayer implementing the MediaPlayer interface. AudioPlayer can play mp3 format audio files by default.&lt;br /&gt;
&lt;br /&gt;
We are having another interface AdvancedMediaPlayer and concrete classes implementing the AdvancedMediaPlayer interface. These classes can play vlc and mp4 format files.&lt;br /&gt;
&lt;br /&gt;
We want to make AudioPlayer to play other formats as well. To attain this, we have created an adapter class MediaAdapter which implements the MediaPlayer interface and uses AdvancedMediaPlayer objects to play the required format.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
https://www.tutorialspoint.com/design_pattern/images/adapter_pattern_uml_diagram.jpg&lt;br /&gt;
&lt;br /&gt;
=Example=&lt;br /&gt;
&lt;br /&gt;
==Step 1-Create interfaces==&lt;br /&gt;
Create interfaces for Media Player and Advanced Media Player.&lt;br /&gt;
&lt;br /&gt;
MediaPlayer.java&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface MediaPlayer {&lt;br /&gt;
   public void play(String audioType, String fileName);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
AdvancedMediaPlayer.java&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public interface AdvancedMediaPlayer {	&lt;br /&gt;
   public void playVlc(String fileName);&lt;br /&gt;
   public void playMp4(String fileName);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 2-Create concrete classes implementing the interface==&lt;br /&gt;
Create concrete classes implementing the AdvancedMediaPlayer interface.&lt;br /&gt;
&lt;br /&gt;
VlcPlayer.java&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class VlcPlayer implements AdvancedMediaPlayer{&lt;br /&gt;
   @Override&lt;br /&gt;
   public void playVlc(String fileName) {&lt;br /&gt;
      System.out.println(&amp;quot;Playing vlc file. Name: &amp;quot;+ fileName);		&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   @Override&lt;br /&gt;
   public void playMp4(String fileName) {&lt;br /&gt;
      //do nothing&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mp4Player.java&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Mp4Player implements AdvancedMediaPlayer{&lt;br /&gt;
&lt;br /&gt;
   @Override&lt;br /&gt;
   public void playVlc(String fileName) {&lt;br /&gt;
      //do nothing&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   @Override&lt;br /&gt;
   public void playMp4(String fileName) {&lt;br /&gt;
      System.out.println(&amp;quot;Playing mp4 file. Name: &amp;quot;+ fileName);		&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 3-Create adapter class implementing the interface==&lt;br /&gt;
Create adapter class implementing the MediaPlayer interface.&lt;br /&gt;
&lt;br /&gt;
MediaAdapter.java&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class MediaAdapter implements MediaPlayer {&lt;br /&gt;
&lt;br /&gt;
   AdvancedMediaPlayer advancedMusicPlayer;&lt;br /&gt;
&lt;br /&gt;
   public MediaAdapter(String audioType){&lt;br /&gt;
   &lt;br /&gt;
      if(audioType.equalsIgnoreCase(&amp;quot;vlc&amp;quot;) ){&lt;br /&gt;
         advancedMusicPlayer = new VlcPlayer();			&lt;br /&gt;
         &lt;br /&gt;
      }else if (audioType.equalsIgnoreCase(&amp;quot;mp4&amp;quot;)){&lt;br /&gt;
         advancedMusicPlayer = new Mp4Player();&lt;br /&gt;
      }	&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   @Override&lt;br /&gt;
   public void play(String audioType, String fileName) {&lt;br /&gt;
   &lt;br /&gt;
      if(audioType.equalsIgnoreCase(&amp;quot;vlc&amp;quot;)){&lt;br /&gt;
         advancedMusicPlayer.playVlc(fileName);&lt;br /&gt;
      }&lt;br /&gt;
      else if(audioType.equalsIgnoreCase(&amp;quot;mp4&amp;quot;)){&lt;br /&gt;
         advancedMusicPlayer.playMp4(fileName);&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 4-Create concrete class implementing the interface.==&lt;br /&gt;
&lt;br /&gt;
Create concrete class implementing the MediaPlayer interface.&lt;br /&gt;
&lt;br /&gt;
AudioPlayer.java&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class AudioPlayer implements MediaPlayer {&lt;br /&gt;
   MediaAdapter mediaAdapter; &lt;br /&gt;
&lt;br /&gt;
   @Override&lt;br /&gt;
   public void play(String audioType, String fileName) {		&lt;br /&gt;
&lt;br /&gt;
      //inbuilt support to play mp3 music files&lt;br /&gt;
      if(audioType.equalsIgnoreCase(&amp;quot;mp3&amp;quot;)){&lt;br /&gt;
         System.out.println(&amp;quot;Playing mp3 file. Name: &amp;quot; + fileName);			&lt;br /&gt;
      } &lt;br /&gt;
      &lt;br /&gt;
      //mediaAdapter is providing support to play other file formats&lt;br /&gt;
      else if(audioType.equalsIgnoreCase(&amp;quot;vlc&amp;quot;) || audioType.equalsIgnoreCase(&amp;quot;mp4&amp;quot;)){&lt;br /&gt;
         mediaAdapter = new MediaAdapter(audioType);&lt;br /&gt;
         mediaAdapter.play(audioType, fileName);&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      else{&lt;br /&gt;
         System.out.println(&amp;quot;Invalid media. &amp;quot; + audioType + &amp;quot; format not supported&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
   }   &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 5-Use concrete class to play different types of formats==&lt;br /&gt;
&lt;br /&gt;
Use the AudioPlayer to play different types of audio formats.&lt;br /&gt;
&lt;br /&gt;
AdapterPatternDemo.java&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class AdapterPatternDemo {&lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
      AudioPlayer audioPlayer = new AudioPlayer();&lt;br /&gt;
&lt;br /&gt;
      audioPlayer.play(&amp;quot;mp3&amp;quot;, &amp;quot;beyond the horizon.mp3&amp;quot;);&lt;br /&gt;
      audioPlayer.play(&amp;quot;mp4&amp;quot;, &amp;quot;alone.mp4&amp;quot;);&lt;br /&gt;
      audioPlayer.play(&amp;quot;vlc&amp;quot;, &amp;quot;far far away.vlc&amp;quot;);&lt;br /&gt;
      audioPlayer.play(&amp;quot;avi&amp;quot;, &amp;quot;mind me.avi&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 6-Output==&lt;br /&gt;
Verify the output.&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
Playing mp3 file. Name: beyond the horizon.mp3&lt;br /&gt;
Playing mp4 file. Name: alone.mp4&lt;br /&gt;
Playing vlc file. Name: far far away.vlc&lt;br /&gt;
Invalid media. avi format not supported&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rasimsen</name></author>
		
	</entry>
</feed>