← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 7 of 14

Java का Adapter Pattern

Adapter pattern एक class के interface को दूसरे interface में बदल देता है जिसकी client code उम्मीद करती है, otherwise incompatible classes को बिना किसी एक को modify किए साथ काम करने देते हुए।
Syntax
java
class Adapter implements TargetInterface {
    private Adaptee adaptee;
    public void request() {
        adaptee.specificRequest();
    }
}

What is the Adapter Pattern?

Adapter pattern किसी मौजूदा class के interface को किसी दूसरे interface में बदल देता है जिसकी client code उम्मीद करती है, otherwise असंगत interfaces वाली classes को बिना किसी एक को modify किए साथ काम करने देते हुए।

उदाहरण: What is the Adapter Pattern?

java
public class Main {
	interface Target { void request(); }
	static class Adaptee { void specificRequest() { System.out.println("Adaptee's own method"); } }
	static class Adapter implements Target {
		Adaptee adaptee = new Adaptee();
		public void request() { adaptee.specificRequest(); } // translates one interface to another
	}
	public static void main(String[] args) {
		Target t = new Adapter();
		t.request();
	}
}

Class Adapter vs Object Adapter

एक class adapter inheritance का उपयोग करके सीधे adaptee को extend करता है, जबकि एक object adapter composition का उपयोग करता है, adaptee instance का एक reference रखते हुए -- Java की single inheritance की सीमाएं ज़्यादातर cases में object adapter approach को प्राथमिकता देती हैं।

उदाहरण: Class Adapter vs Object Adapter

java
public class Main {
	static class Adaptee { void specific() { System.out.println("specific()"); } }
	interface Target { void request(); }
	static class ObjectAdapter implements Target { // composition -- holds a reference
		Adaptee adaptee = new Adaptee();
		public void request() { adaptee.specific(); }
	}
	public static void main(String[] args) {
		new ObjectAdapter().request(); // Java favors this over class-adapter inheritance
	}
}

Adapting a Legacy Interface

Adapters खासतौर पर तब आम हैं जब legacy code या किसी third-party library के साथ integrate करना हो जिसका API बाकी application की उम्मीद से मेल नहीं खाता, उस mismatch को एक single translation class के पीछे isolate करते हुए।

उदाहरण: Adapting a Legacy Interface

java
public class Main {
	static class LegacyPrinter { void oldPrint(String s) { System.out.println("Legacy: " + s); } }
	interface ModernPrinter { void print(String s); }
	static class PrinterAdapter implements ModernPrinter {
		LegacyPrinter legacy = new LegacyPrinter();
		public void print(String s) { legacy.oldPrint(s); } // isolates the mismatch here
	}
	public static void main(String[] args) {
		ModernPrinter p = new PrinterAdapter();
		p.print("Hello");
	}
}

Adapting Data Formats

सिर्फ methods का नाम बदलने के अलावा, एक adapter data को formats या units के बीच पूरी तरह convert भी कर सकता है, जैसे XML को JSON में, या Fahrenheit को Celsius में translate करना, callers को एक साफ़ target interface प्रस्तुत करते हुए।

उदाहरण: Adapting Data Formats

java
public class Main {
	interface TemperatureReader { double celsius(); }
	static class FahrenheitSensor { double readFahrenheit() { return 98.6; } }
	static class TemperatureAdapter implements TemperatureReader {
		FahrenheitSensor sensor = new FahrenheitSensor();
		public double celsius() { return (sensor.readFahrenheit() - 32) * 5 / 9; } // converts units, not just names
	}
	public static void main(String[] args) {
		System.out.println(new TemperatureAdapter().celsius());
	}
}

Adapter vs Facade

Adapter और Facade दोनों दूसरे code को wrap करते हैं, लेकिन Adapter किसी एक specific असंगत interface को उम्मीद के अनुसार fit कराने के लिए मौजूद है, जबकि Facade एक पूरे जटिल subsystem तक access को एक single simplified interface के पीछे सरल बनाने के लिए मौजूद है।

उदाहरण: Adapter vs Facade

java
public class Main {
	interface Target { void request(); } // Adapter: makes ONE incompatible interface fit
	static class SubsystemA { void a() { System.out.println("A"); } }
	static class SubsystemB { void b() { System.out.println("B"); } }
	static class Facade { // Facade: simplifies access to a WHOLE subsystem
		void doEverything() { new SubsystemA().a(); new SubsystemB().b(); }
	}
	public static void main(String[] args) {
		new Facade().doEverything();
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. मौजूदा (adaptee) class को नए interface से मेल खाने के लिए बदलना, जबकि adapter को इसे wrap करके बिना बदले छोड़ना चाहिए।
  2. Adapter को Facade से confuse करना: एक Adapter एक असंगत interface को दूसरे में फिट कराता है, जबकि एक Facade पूरे subsystem को सरल बनाता है।
  3. adapter में units या formats जैसे data को convert करना भूल जाना, जिससे wrapped call काम तो करती है लेकिन client की उम्मीद से अलग values return करती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.