Adapter Design Pattern
Arnob Deb
Lecturer, Daffodil International University
What is Adapter Design Pattern?
• - Structural design pattern that allows
incompatible interfaces to work together.
• - Acts as a bridge between two incompatible
classes.
• - Converts one interface into another that a
client expects.
Bonus Answer
• // 🎯 Target Interface (Expected Format - European Plug)
• interface EuropeanPlug {
• void connectEuropeanSocket();
• }
• // 🔌 Adaptee (Existing but incompatible - US Plug)
• class USPlug {
• public void connectUSSocket() {
• System.out.println("Connected to US socket.");
• }
• }
• // 🔄 Adapter (Converts US Plug into European Plug)
• class PlugAdapter implements EuropeanPlug {
• private USPlug usPlug; // Store the US Plug
• public PlugAdapter(USPlug usPlug) {
• this.usPlug = usPlug; // Initialize with the US plug
• }
• @Override
• public void connectEuropeanSocket() {
• System.out.println("Adapter converts US plug to fit European socket.");
• usPlug.connectUSSocket(); // Calls the existing US plug method
• }
• }
• // 🚀 Client Code (Using the adapter)
• public class AdapterExample {
• public static void main(String[] args) {
• USPlug usPlug = new USPlug(); // Create a US plug
• EuropeanPlug adapter = new PlugAdapter(usPlug); // Use adapter
• adapter.connectEuropeanSocket(); // Now it works in a European socket!
• }
• }