🚀 Enjoying Java Tutorials?
Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!
🔔 Subscribe to Ram N JavaIntroduction
The Adapter Design Pattern is a structural design pattern that allows classes with incompatible interfaces to collaborate. A very natural real-world metaphor for this pattern is a Language Translator: imagine a person who speaks English wanting to communicate with someone who speaks French. Without an intermediary, direct communication is impossible. A translator acts as an adapter, listening to English, translating it to French, and vice versa.
Adapter Pattern Mapping
| Pattern Role | Component in Example | Description |
|---|---|---|
| Client | EnglishSpeaker |
Understands and speaks English words only |
| Target Interface | ITarget |
Defines the interface expected by the client (e.g., translateAndTalk()) |
| Adaptee | FrenchSpeaker |
Existing class that understands only French |
| Adapter | TranslatorAdapter |
Translates messages between English and French seamlessly |
Java Code Implementation
1. Adaptee: FrenchSpeaker
public class FrenchSpeaker {
public void speakFrench(String words) {
System.out.println("French speaker receives: " + words);
}
public String replyInFrench() {
return "Très bien, merci!";
}
}
2. Target Interface: ITarget
public interface ITarget {
void speakEnglish(String words);
String receiveReplyInEnglish();
}
3. Adapter: TranslatorAdapter
public class TranslatorAdapter implements ITarget {
private FrenchSpeaker frenchSpeaker = new FrenchSpeaker();
@Override
public void speakEnglish(String words) {
System.out.println("Adapter translates English: \"" + words + "\" to French");
String frenchTranslation = convertToFrench(words);
frenchSpeaker.speakFrench(frenchTranslation);
}
@Override
public String receiveReplyInEnglish() {
String frenchReply = frenchSpeaker.replyInFrench();
System.out.println("Adapter receives French reply: \"" + frenchReply + "\"");
return convertToEnglish(frenchReply);
}
private String convertToFrench(String englishWords) {
if ("Hello, how are you?".equalsIgnoreCase(englishWords)) {
return "Bonjour, comment allez-vous?";
}
return "Bonjour!";
}
private String convertToEnglish(String frenchWords) {
if ("Très bien, merci!".equalsIgnoreCase(frenchWords)) {
return "Very well, thank you!";
}
return "Thank you!";
}
}
4. Client: EnglishSpeaker
public class EnglishSpeaker {
public static void main(String[] args) {
ITarget translator = new TranslatorAdapter();
System.out.println("English Speaker says: Hello, how are you?\n");
translator.speakEnglish("Hello, how are you?");
System.out.println();
String reply = translator.receiveReplyInEnglish();
System.out.println("English Speaker hears: " + reply);
}
}
Console Output
English Speaker says: Hello, how are you?
Adapter translates English: "Hello, how are you?" to French
French speaker receives: Bonjour, comment allez-vous?
Adapter receives French reply: "Très bien, merci!"
English Speaker hears: Very well, thank you!
Related Tutorials from Ram N Java
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);
}
}
/*
* 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
See also:
No comments:
Post a Comment