Convert the interface of a class into another interface clients expect. You need to integrate a legacy or third-party API. You want to reuse
Adapter
Overview
Convert the interface of a class into another interface clients expect.
When to use
- You need to integrate a legacy or third-party API.
- You want to reuse existing classes without modifying them.
Java example
TypeScript example
Pros and cons
Pros:
- Enables reuse without changing original code.
- Keeps client code stable.
Cons:
- Adds an extra abstraction layer.
- Too many adapters can complicate the codebase.
Common pitfalls
- Over-adapting when a simpler wrapper works.
- Hiding breaking behavior differences.