What types of adapters exist?
There are three main types of adapters: class, object, and two-way. Each implements the idea of compatibility in its own way.
1. Class Adapter
Implemented via inheritance: the adapter inherits the client's interface and at the same time inherits (or implements) the adapted class. Used where the language supports multiple inheritance (for example, C++).
Scheme: Client → Adapter (inherits Target and Adaptee) → Adaptee
Characteristics:
- High performance (no delegation).
- Cannot be applied without multiple inheritance.
- Tight coupling to the adapted class.
Example (C++):
class Adapter : public Target, private Adaptee {
public:
void request() override {
specificRequest();
}
};2. Object Adapter
Uses composition: the adapter holds a reference to the adapted object internally and delegates calls to it. This is the most common variant in Java, C#, and Python.
Scheme: Client → Adapter (contains Adaptee) → Adaptee
Characteristics:
- Flexibility: different objects can be adapted at runtime.
- Less coupling (the adapter does not depend on a specific class).
- Slightly higher overhead due to delegation.
Example (Java):
class Adapter implements Target {
private Adaptee adaptee = new Adaptee();
public void request() {
adaptee.specificRequest();
}
}3. Two-Way Adapter
Allows mutual compatibility: the client can work through both the Target interface and the Adaptee interface.
That is, the same adapter can translate calls in both directions.
Used:
- when integrating two legacy systems where both sides need to be made to "get along";
- in network protocols, API gateways, drivers.
Characteristics:
- More complex implementation.
- Often used at the middleware level and in bidirectional interfaces.
4. Conclusion
| Adapter type | Principle | Relation to Adaptee | Advantages | Disadvantages |
|---|---|---|---|---|
| Class | Inheritance | Tight | Faster, simpler | Inflexible, requires multiple inheritance |
| Object | Composition | Reference | Flexible, safe | Slower, more code |
| Two-way | Both sides | Mutual | Universal | Complex to implement |
Summary: Class adapter is fast but rigid. Object adapter is flexible and safe (the most common). Two-way adapter is universal but complex.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.