Skip to main content

Adapter Pattern

In Turkish
Adapter Deseni
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/adapter-pattern

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

Adapting a third-party SDK to the app's own interfacetypescript
// 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings