How to Implement Adapter Design Pattern in Java


In this example we will show the method to implement Adapter Pattern in Java.

Source Code

package com.beginner.examples;

interface German {  
    void speakGerman(String string);  
}  
 
class English {  
    void speakEnglish(String string) { 
    	System.out.println("Good"); 
    }  
}  
  
// adapter
class Translator implements German{  
    private English english = new English();  
  
    Translator(English english) { 
    	this.english = english; 
    }  
  
    @Override 
    public void speakGerman(String string) { 
    	english.speakEnglish(string); 
    }  
}  
 
public class AdapterExample {  
    public static void main(String[] args) {  
        German german = new Translator(new English());  
        german.speakGerman("Gut");
    }  
}

Output:

Good
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments