Structural PatternsAdapter PatternMedium⏱️ ~2 min

Adapter Pattern Structure and Participants

The Adapter Pattern has two common implementations: Class Adapter (uses inheritance) and Object Adapter (uses composition). Object Adapter is more commonly used because it follows the composition over inheritance principle.

Object Adapter Structure (Recommended)

«interface»
Target
+ request(): void
▲ implements
Adapter
- adaptee: Adaptee
+ request(): void
◆ contains
Adaptee
+ specificRequest(): void

Participants and Responsibilities

Target Interface: Defines the domain-specific interface that the Client uses. This is what the client code expects to work with.

Adapter: Implements the Target interface and holds a reference to the Adaptee. It translates calls from the Target interface to the Adaptee's interface. The translation logic resides here.

Adaptee: The existing class with an incompatible interface that needs adapting. It contains useful functionality but cannot be used directly by the client.

Client: Collaborates with objects conforming to the Target interface. It remains unaware of the Adapter's existence or the Adaptee's incompatible interface.

Call Flow

1. Client calls adapter.request()
2. Adapter translates the call
3. Adapter calls adaptee.specificRequest()
4. Adaptee performs the work
5. Adapter returns result to Client (if needed)
Interview Tip: Always clarify whether to use Class Adapter (multiple inheritance, if language supports) or Object Adapter (composition). Object Adapter is preferred in most languages and follows SOLID principles better.
💡 Key Takeaways
Object Adapter uses composition (◆) to hold Adaptee reference
Class Adapter uses inheritance (▲) from both Target and Adaptee
Adapter implements Target interface and delegates to Adaptee
Client depends only on Target interface, not concrete Adapter
Translation logic is encapsulated within the Adapter class
📌 Examples
1Java's Arrays.asList() adapts arrays to List interface
2InputStreamReader adapts InputStream to Reader interface
← Back to Adapter Pattern Overview
Adapter Pattern Structure and Participants | Adapter Pattern - System Overflow