Adapter Pattern
- In Turkish
- Adapter Deseni
In short
The adapter pattern is a structural design pattern that wraps an existing class in a new interface, so code expecting one interface can use an incompatible one.
What is the adapter pattern?
The adapter pattern connects two pieces of code whose interfaces don't match. Your code expects one interface, called the target, but the class you want to use, called the adaptee, has different method names, parameters, or data formats. An adapter is a small class that implements the target interface and translates each call into the form the adaptee understands, so neither side has to change.
Most adapters use composition: the adapter holds a reference to the adaptee and forwards translated calls to it, which is called an object adapter. In languages with multiple inheritance, a class adapter can inherit from both sides instead. The translation might mean renaming methods, converting units such as dollars to cents, reshaping data from XML to JSON, or turning callbacks into promises.
The everyday analogy is a travel plug adapter: your charger has a US plug and the wall has a European socket, and the adapter lets them work together without rewiring either one. In software, adapters are used to wrap third-party libraries and SDKs, integrate legacy code, support several vendors behind one interface, and, in hexagonal architecture, connect the application's ports to databases, APIs, and user interfaces.
The adapter is often confused with the decorator, facade, and proxy patterns, which also wrap other objects. An adapter changes an interface to make it compatible, a decorator keeps the same interface and adds behavior, a facade offers a new, simpler interface over a whole subsystem, and a proxy keeps the same interface while controlling access, for example by adding caching or lazy loading. The deciding question is whether the wrapper's job is translation, extra behavior, simplification, or control.
Key takeaways
- An adapter translates one interface into another that client code expects.
- It lets incompatible classes work together without changing either one.
- Object adapters use composition; class adapters use inheritance.
- Decorators add behavior, facades simplify, and proxies control access; adapters translate.
- Hexagonal architecture uses adapters to connect the core to external systems.
Example
// The interface our app expects: amounts in dollars
interface PaymentGateway {
pay(amountDollars: number, email: string): Promise<void>;
}
// Adapter: makes a third-party SDK with a different interface fit ours
class LegacyPayAdapter implements PaymentGateway {
constructor(private sdk: { createCharge(opts: { cents: number; customer: string }): Promise<void> }) {}
pay(amountDollars: number, email: string) {
// Translate units and parameter names for the SDK
return this.sdk.createCharge({ cents: Math.round(amountDollars * 100), customer: email });
}
}Readers ask
What is the difference between the adapter and facade patterns?
An adapter makes an existing interface match one that clients already expect, usually by wrapping a single class. A facade creates a new, simpler interface over a whole group of classes to make a complex subsystem easier to use.
What is the difference between the adapter and decorator patterns?
An adapter changes the interface so incompatible code can work together. A decorator keeps exactly the same interface and wraps an object to add behavior, such as logging or caching.
How is the adapter pattern related to hexagonal architecture?
Hexagonal architecture, also called ports and adapters, applies the same idea at the level of a whole application. The core defines ports as interfaces, and adapters translate between those ports and specific technologies such as a SQL database or a REST API.
See also
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
- Hexagonal ArchitectureSoftware Architecture, p. 21Hexagonal architecture is a way of structuring software so the core business logic talks to the outside world only through ports and swappable adapters.
- InterfaceProgramming Fundamentals, p. 29An interface is a named set of method and property signatures that a type promises to provide, without saying how those members are implemented.
- Loose CouplingSoftware Architecture, p. 26Loose coupling is a design principle in which components depend on each other as little as possible, so one can change without breaking the others.
- Factory PatternSoftware Architecture, p. 19The factory pattern is a creational design pattern that moves object creation into a dedicated function or class, so callers never name the concrete class.
- Strategy PatternSoftware Architecture, p. 44The strategy pattern is a behavioral design pattern that puts interchangeable algorithms behind one interface, so code can switch between them at run time.
- Decorator PatternSoftware Architecture, p. 11The decorator pattern adds behavior like logging or caching to an object by wrapping it in another with the same interface, without changing the original.
Spotted a mistake or something missing on this page?Suggest an edit