Saturday, 2 November 2013

Adapter Design Pattern – Implementation [Language Translator]


Click here to watch in Youtube : https://www.youtube.com/watch?v=oudJQOQU-i8

Click the below Image to Enlarge
Adapter Design Pattern – Implementation [Language Translator]






















Adapter Design Pattern – Implementation [Language Translator] - Class Diagram



























ITarget.java

public interface ITarget
{
public String translateAndTellToOtherPerson(String words,String convertToWhichLanguage);
}

John.java

import java.util.HashMap;

/*
 *  John can speak and understand English and French,he acts as a Adapter or Translator
 */
public class John implements ITarget
{

static HashMap<String, String> englishFrenchMap = new HashMap<String, String>();
static HashMap<String, String> frenchEnglishMap = new HashMap<String, String>();
David david = new David();
Rohit rohit =new Rohit();

static
{
englishFrenchMap.put("how are you?", "comment allez-vous?");
englishFrenchMap.put("I am in USA", "Je suis aux Etats-Unis");
frenchEnglishMap.put("Je suis très bien", "I am fine");
frenchEnglishMap.put("où êtes-vous?", "where are you?");

}

@Override
public String translateAndTellToOtherPerson( String words, String convertToWhichLanguage )
{
if( convertToWhichLanguage.equalsIgnoreCase("English") )
{

String englishWords = convertToEnglish(words);
System.out.println("\nJohn Converted \"" + words + " \" to \"" + englishWords
               + " and send the question to David");
String englishWordsReply = david.answerFortheQuestion(englishWords);
System.out.println("John Got reply from David in English like : " + "\"" + englishWordsReply
               + "\"");
String frenchConverted = convertToFrench(englishWordsReply);
System.out.println("John Converted " + "\"" + englishWordsReply + "\"" + " to " + "\""
               + frenchConverted + "\"" + " and send back to Rohit\n");
return frenchConverted;

}
else if( convertToWhichLanguage.equalsIgnoreCase("French") )

{
String frenchWords = convertToFrench(words);
System.out.println("\nJohn Converted \"" + words + " \" to \"" + frenchWords
               + " and send the question to Rohit");
String frenchWordsReply =rohit.answerFortheQuestion(frenchWords);
System.out.println("John Got reply from Rohit in French like : " + "\"" + frenchWordsReply
               + "\"");
String englishConverted = convertToEnglish(frenchWordsReply);
System.out.println("John Converted " + "\"" + frenchWordsReply + "\"" + " to " + "\""
               + englishConverted + "\"" + " and send back to David\n");
return englishConverted;

}

return "Sorry Cannot Covert";
}

public String convertToFrench( String words )
{
return englishFrenchMap.get(words);
}

public String convertToEnglish( String words )
{
return frenchEnglishMap.get(words);
}

}


EnglishSpeaker.java

public interface EnglishSpeaker
{
public String askQuestion( String words );
public String answerFortheQuestion( String words );

}

David.java
/*
 * He is from USA, So he can speak and understand only English
 */
public class David implements EnglishSpeaker
{

public static void main( String args[] )
{
String replyFromRohit = new David().askQuestion("how are you?");
System.out.println("Reply From Rohit [French Speaker can Speak and Understand only French] :  " + replyFromRohit);
}

@Override
public String askQuestion( String words )
{
System.out.println("Question Asked by David [English Speaker and Can understand only English] : " +words);
ITarget target = new John();
String replyFromRohit = target.translateAndTellToOtherPerson(words, "French");
return replyFromRohit;
}

@Override
public String answerFortheQuestion( String words )
{
String reply = null;
if( words.equalsIgnoreCase("where are you?") )
{
reply = "I am in USA";
}
return reply;
}

}


FrenchSpeaker.java

public interface FrenchSpeaker
{
public String askQuestion( String words );
public String answerFortheQuestion( String words );

}


Rohit.java

/*
 * He is from France, So he can speak and understand only French
 */
public class Rohit implements FrenchSpeaker
{

public static void main( String args[] )
{
String replyFromDavid = new Rohit().askQuestion("où êtes-vous?");
System.out.println("Reply From David [English Speaker can Speak and Understand only English] :  " + replyFromDavid);
}

@Override
public String askQuestion( String words )
{
System.out.println("Question Asked by Rohit [French Speaker and Can understand only French] : " +words);
ITarget target = new John();
String replyFromDavid = target.translateAndTellToOtherPerson(words, "English");
return replyFromDavid;
}

@Override
public String answerFortheQuestion( String words )
{
String reply = null;
if( words.equalsIgnoreCase("comment allez-vous?") )
{
reply = "Je suis très bien";
}
return reply;
}

}

Output

David asks Question to Rohit

Question Asked by David [English Speaker and Can understand only English] : how are you?

John Converted "how are you? " to "comment allez-vous? and send the question to Rohit
John Got reply from Rohit in French like : "Je suis très bien"
John Converted "Je suis très bien" to "I am fine" and send back to David

Reply From Rohit [French Speaker can Speak and Understand only French] :  I am fine

Rohit asks Question to David

Question Asked by Rohit [French Speaker and Can understand only French] : où êtes-vous?

John Converted "où êtes-vous? " to "where are you? and send the question to David
John Got reply from David in English like : "I am in USA"
John Converted "I am in USA" to "Je suis aux Etats-Unis" and send back to Rohit

Reply From David [English Speaker can Speak and Understand only English] :  Je suis aux Etats-Unis

No comments:

Post a Comment