0% found this document useful (0 votes)
8 views13 pages

Adapter Design Pattern

The Adapter Design Pattern is a structural design pattern that enables incompatible interfaces to work together by acting as a bridge between them. It converts one interface into another that a client expects, exemplified by an adapter that allows a US plug to connect to a European socket. The document includes code snippets demonstrating the implementation of this pattern with a target interface, an adaptee, and an adapter class.

Uploaded by

jabikoi314
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Adapter Design Pattern

The Adapter Design Pattern is a structural design pattern that enables incompatible interfaces to work together by acting as a bridge between them. It converts one interface into another that a client expects, exemplified by an adapter that allows a US plug to connect to a European socket. The document includes code snippets demonstrating the implementation of this pattern with a target interface, an adaptee, and an adapter class.

Uploaded by

jabikoi314
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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!
• }
• }

You might also like