Structural Patterns • Adapter PatternEasy⏱️ ~2 min
What is the Adapter Pattern?
Definition
Adapter Pattern is a structural design pattern that allows incompatible interfaces to work together by wrapping an existing class with a new interface that the client expects.
The Adapter Pattern solves the problem of interface incompatibility. When you have an existing class (the Adaptee) that provides useful functionality but its interface does not match what your client code expects, the Adapter acts as a translator between the two.
Core Problem It Solves:
First, it enables reuse of existing classes without modifying their source code. Second, it decouples the client from the concrete implementation of the service. Third, it allows you to introduce new types of adapters without breaking existing client code.
Client
expects Target interface
↓ uses
Adapter
implements Target
↓ wraps
Adaptee
has incompatible interface
Real-World Analogy: A power adapter for travel. Your US laptop plug (client) expects a US outlet interface (target), but the European outlet (adaptee) has a different interface. The power adapter translates between the two without modifying either the plug or the outlet.
💡 Key Takeaways
✓Converts one interface into another interface that clients expect
✓Allows classes with incompatible interfaces to work together
✓Wraps an existing class without modifying its source code
✓Client code remains decoupled from the concrete Adaptee implementation
📌 Examples
1Converting between different data formats (XML to JSON)
2Integrating third-party libraries with incompatible APIs
3Adapting legacy code to work with new systems