How Adapter Pattern Solves Interface Mismatches in Real-World Scenarios
This article explains the Adapter Pattern, its core roles, and demonstrates three practical applications—object adapter for document formats, class adapter for component replacement, and interface adapter for large interfaces—complete with Java code examples and key takeaways.
What Is the Adapter Pattern?
The Adapter Pattern converts the interface of an existing class into another interface that a client expects, enabling incompatible objects to work together without modifying their source code.
Key Roles
Target : Abstract class defining the interface the client uses.
Adapter : Class that adapts the Adaptee to the Target interface.
Adaptee : The existing class whose interface needs conversion.
Client : Uses the Target interface to interact with the adapted functionality.
1. Object Adapter in Document Processing
Microsoft Office supports OOXML (2007) and binary (2003) formats. An existing component handles OOXML; to add 2003 support without rewriting the component, an object adapter wraps the OOXML class and presents a 2003‑compatible interface.
// Document client
public class Document {
public void view(IDoc2003 doc) {
doc.show();
}
}
// 2003 interface
public interface IDoc2003 {
void show();
}
// 2007 component (adaptee)
public class Doc2007 {
public void show() {
System.out.println("office2007 standard processing");
}
}
// Adapter converting 2007 to 2003
public class DocAdapter implements IDoc2003 {
private Doc2007 doc2007;
public DocAdapter(Doc2007 doc2007) {
this.doc2007 = doc2007;
}
@Override
public void show() {
System.out.println("Adapter: delegating to 2007 implementation");
doc2007.show();
}
}
// Test
public class Test {
public static void main(String[] args) {
Document document = new Document();
Doc2003 doc2003 = new DocAdapter(new Doc2007());
document.view(doc2003);
}
}2. Class Adapter for Replacing Dependent Components
When a library is upgraded, method signatures often change, requiring widespread code changes. A class adapter can inherit from the new component and expose the old interface, minimizing modifications.
// Old abstract handler (target)
public class AHandler {
public void operation() {}
}
// New component (adaptee)
public class BHandler {
public void action() {}
}
// Adapter using inheritance
public class BAdapter extends BHandler {
public void operation() {
// additional logic if needed
super.action();
}
}
// Client code
public class Client {
public static void main(String[] args) {
// Original call
AHandler a = new AHandler();
a.operation();
// New call via adapter
BAdapter b = new BAdapter();
b.operation();
}
}3. Interface Adapter for Large Interfaces
When an interface defines many methods but a concrete class only needs a few, implementing all methods becomes cumbersome. An abstract adapter provides empty implementations, allowing subclasses to override only the methods they care about.
// Large target interface
public interface TargetObj {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
// Abstract adapter with default (empty) implementations
public abstract class Adapter implements TargetObj {
public void operation1() {}
public void operation2() {}
public void operation3() {}
public void operation4() {}
public void operation5() {}
}
// Client using anonymous subclasses to implement needed methods only
public class Client {
public static void main(String[] args) {
Adapter adapter1 = new Adapter() {
@Override
public void operation3() {
System.out.println("operation3");
}
};
Adapter adapter2 = new Adapter() {
@Override
public void operation5() {
System.out.println("operation5");
}
};
adapter1.operation3();
adapter2.operation5();
}
}Conclusion
Object Adapter – uses composition to adapt an existing object.
Class Adapter – uses inheritance or interface implementation to adapt a class.
Interface Adapter – provides default (empty) implementations so subclasses override only needed methods.
Adapter Pattern is powerful when existing classes cannot be changed, but it should be used judiciously because it can reduce code readability and increase maintenance overhead.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
