Java का Adapter Pattern
In this page:
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?
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
Adapting a Legacy Interface
Adapters खासतौर पर तब आम हैं जब legacy code या किसी third-party library के साथ integrate करना हो जिसका API बाकी application की उम्मीद से मेल नहीं खाता, उस mismatch को एक single translation class के पीछे isolate करते हुए।
उदाहरण: Adapting a Legacy Interface
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");
}
}
Login to try C/C++/Java/PHP code in the editor
Adapting Data Formats
सिर्फ methods का नाम बदलने के अलावा, एक adapter data को formats या units के बीच पूरी तरह convert भी कर सकता है, जैसे XML को JSON में, या Fahrenheit को Celsius में translate करना, callers को एक साफ़ target interface प्रस्तुत करते हुए।
उदाहरण: Adapting Data Formats
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());
}
}
Login to try C/C++/Java/PHP code in the editor
Adapter vs Facade
Adapter और Facade दोनों दूसरे code को wrap करते हैं, लेकिन Adapter किसी एक specific असंगत interface को उम्मीद के अनुसार fit कराने के लिए मौजूद है, जबकि Facade एक पूरे जटिल subsystem तक access को एक single simplified interface के पीछे सरल बनाने के लिए मौजूद है।
उदाहरण: Adapter vs Facade
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();
}
}
Login to try C/C++/Java/PHP code in the editor
- मौजूदा (adaptee) class को नए interface से मेल खाने के लिए बदलना, जबकि adapter को इसे wrap करके बिना बदले छोड़ना चाहिए।
- Adapter को Facade से confuse करना: एक Adapter एक असंगत interface को दूसरे में फिट कराता है, जबकि एक Facade पूरे subsystem को सरल बनाता है।
- adapter में units या formats जैसे data को convert करना भूल जाना, जिससे wrapped call काम तो करती है लेकिन client की उम्मीद से अलग values return करती है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern