Tuesday 26 August 2014

Adapter Design Pattern Implementation

Adapter Design Pattern

Adapter design pattern is useful for a conversion program. That means it will be used to make two interfaces and classes compatible so that combinedly they can work. For more details please refer the given link : http://www.javatechtipssharedbygaurav.com/2014/03/java-design-patterns-part-2.html

In real time, we are using different varieties of LED TV in our home. When we want to play some videos in those LED TV then sometime it will throw exception saying that file is not supported by the device. Like the MKV file extension is not supported by the SonyLED but if we will use PS3Muxer software then this program will convert it into compatible extension and that file will be played by the SonyLED. Below is the demonstration for how to implement this concept in programming.

1. Playable.java

package com.gaurav.designpattern.adapter;

/**
 * @author gaurav
 *
 */
public interface Playable {
public boolean isPlayable(String extensionType);
}

2. Extension.java

package com.gaurav.designpattern.adapter;
/**
 * @author gaurav
 *
 */
enum Extension {
MatroskaVideo("MKV"),
MovingPictureExpertsGroup_4("MP4"),
AudioVideoInterleave("AVI");
private String extensionType;
Extension(String extType){
this.extensionType = extType;
}
String getExtensionType(){
return extensionType;
}
}

3. SonyLED.java

package com.gaurav.designpattern.adapter;
/**
 * @author gaurav
 *
 */
public class SonyLED implements Playable{

@Override
public boolean isPlayable(String extensionType) {
if (extensionType.equals(Extension.MatroskaVideo.getExtensionType()))
return false;
else if (extensionType.equals(Extension.AudioVideoInterleave
.getExtensionType()))
return false;
else
return true;
}
}

4. SamsungLED.java

package com.gaurav.designpattern.adapter;
/**
 * @author gaurav
 *
 */
public class SamsungLED implements Playable{

@Override
public boolean isPlayable(String extensionType) {
if (extensionType.equals(Extension.MatroskaVideo.getExtensionType()))
return true;
else if (extensionType.equals(Extension.AudioVideoInterleave
.getExtensionType()))
return false;
else
return true;
}
}

5. PS3MuxerSonyAdapter.java

package com.gaurav.designpattern.adapter;
/**
 * @author gaurav
 *
 */
public class PS3MuxerSonyAdapter implements Playable{
SonyLED sonyLED;
public PS3MuxerSonyAdapter(SonyLED sonyled){
this.sonyLED = sonyled;
}
@Override
public boolean isPlayable(String extensionType) {
boolean flag = false;
if (extensionType.equals(Extension.MatroskaVideo.getExtensionType())){
System.out.println(extensionType+ " extension is converted during muxing in .m2ts");
flag = true;
}
return flag;
}
public String toString(){
return "Extension is coverted and made compatible with SonyLED";
}
}

6. AviMuxerSamsungAdapter.java

package com.gaurav.designpattern.adapter;
/**
 * @author gaurav
 *
 */
public class AviMuxerSamsungAdapter implements Playable{
SamsungLED samsungLED;
public AviMuxerSamsungAdapter(SamsungLED samsungled){
this.samsungLED = samsungled;
}
@Override
public boolean isPlayable(String extensionType) {
boolean flag = false;
if (extensionType.equals(Extension.AudioVideoInterleave.getExtensionType())){
System.out.println(extensionType+ " extension is converted during muxing in muxed avi");
flag = true;
}
return flag;
}
public String toString(){
return "Extension is coverted and made compatible with SamsungLED";
}
}

7. AdapterDesignPatternDemo.java

package com.gaurav.designpattern.adapter;
/**
 * @author gaurav
 *
 */
public class AdapterDesignPatternDemo {
public static void main(String args[]) {

String sonyExtensionType = "MKV";
SonyLED sonyLED = new SonyLED();
boolean sonyExtensionSupportedFlag = sonyLED.isPlayable(sonyExtensionType);
System.out.println(sonyExtensionType+" file format supported by "
+ sonyLED.getClass().getSimpleName() + " : "
+ sonyExtensionSupportedFlag);
PS3MuxerSonyAdapter ps3MuxerSonyAdapter = new PS3MuxerSonyAdapter(
sonyLED);
sonyExtensionSupportedFlag = ps3MuxerSonyAdapter.isPlayable(sonyExtensionType);
System.out.println(sonyExtensionType + " file format supported  after conversion by "
+ sonyLED.getClass().getSimpleName() + " : "
+ sonyExtensionSupportedFlag);

System.out.println("------------------------------------------------");
String samsungExtensionType = "AVI";
SamsungLED samsungLED = new SamsungLED();
boolean samsungExtensionSupportedFlag = samsungLED.isPlayable(samsungExtensionType);
System.out.println(samsungExtensionType+ " file format supported by "
+ samsungLED.getClass().getSimpleName() + " : "
+ samsungExtensionSupportedFlag);
AviMuxerSamsungAdapter aviMuxerSamsungAdapter = new AviMuxerSamsungAdapter(
samsungLED);
samsungExtensionSupportedFlag = aviMuxerSamsungAdapter
.isPlayable(samsungExtensionType);
System.out.println(samsungExtensionType+ " file format supported after conversion by "
+ samsungLED.getClass().getSimpleName() + " : "
+ samsungExtensionSupportedFlag);
}
}

Result:- 

MKV file format supported by SonyLED : false
MKV extension is converted during muxing in .m2ts
MKV file format supported  after conversion by SonyLED : true
------------------------------------------------
AVI file format supported by SamsungLED : false
AVI extension is converted during muxing in muxed avi
AVI file format supported after conversion by SamsungLED : true

Note:- Here Muxer is working like an adapter which is making extension compatible to TV's

No comments:

Post a Comment