Skip to content

Adapter vs Bridge

Difficulty: Advanced

Prerequisites

Key concepts

  • adapter pattern
  • bridge pattern
  • retrofit
  • implementor interface
  • choosing a pattern
  • Why Adapter and Bridge look alike: in both, one piece of code reaches another through an interface.
  • What separates them: when the interface is designed, who designs it, and how many things vary on each side of it.
  • How each one holds the object on the other side, in C++.
  • Where the two meet in one design, and when a plain interface is all you need.
  • An adapter is a retrofit. Two pieces of code already exist and do not fit: the shop wants PaymentGateway::charge, and the vendor offers oldpay::Terminal::authorize, with different units, codes, and errors. OldPayGateway translates between them. Nobody planned this class; it exists because the two sides were written separately.
  • A bridge is a separation made on purpose. A design varies in two independent ways, shapes and output formats, and would otherwise need one class for every pair. So it is split into two hierarchies, Shape and Canvas, joined by one small interface that the designer chose: line, circle, text. Both sides are written to fit it.

A short test: an adapter is something you add when code does not fit; a bridge is something you design so that two kinds of code never need to know each other’s concrete classes.

The interfaces at the centre of each example:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

payments.hpp
// What the shop needs from any payment service. This is the "target"
// interface: the shape the shop's code wants. It is an abstract class
// (an interface), so code holds a PaymentGateway& and never knows which
// vendor is behind it.
class PaymentGateway {
public:
// Virtual, so destroying a gateway through a PaymentGateway pointer
// also runs the derived class's destructor.
virtual ~PaymentGateway() = default;
// Takes one payment and reports the outcome. `= 0` makes it pure
// virtual: every concrete gateway must write its own charge, and
// PaymentGateway itself cannot be created.
virtual ChargeResult charge(const ChargeRequest& request) = 0;
protected:
// Protected, so only derived classes can use them. That stops
// `gateway_a = gateway_b` through two PaymentGateway references, which
// would copy only the base part and none of the derived class's data.
PaymentGateway() = default;
PaymentGateway(const PaymentGateway&) = default;
PaymentGateway& operator=(const PaymentGateway&) = default;
};

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

oldpay_adapter.hpp
// Lets the shop charge cards through OldPay's Terminal, using the shop's
// own PaymentGateway interface. This is the adapter: on the outside it is
// a PaymentGateway (it inherits from it); on the inside it holds a
// Terminal and translates each call into the vendor's terms and back.
// `final` means no class can derive from it in turn.
class OldPayGateway final : public PaymentGateway {
public:
// Borrows the terminal, which must outlive this object. The adapter
// does not own or create the Terminal: the caller makes one and passes
// it in by reference, and the adapter keeps its address. Taking a
// non-const reference (not a pointer) means a null terminal cannot be
// passed, and a temporary such as oldpay::Terminal{"x"} is refused by
// the compiler. `explicit` stops a Terminal from silently turning into
// an OldPayGateway.
explicit OldPayGateway(oldpay::Terminal& terminal) : terminal_{&terminal} {}
// Translates the request, calls the vendor, and translates the answer.
// Throws std::invalid_argument for an amount of zero or less, before
// calling the vendor. `override` asks the compiler to check that this
// really replaces PaymentGateway::charge.
ChargeResult charge(const ChargeRequest& request) override;
private:
// The borrowed terminal. A pointer rather than a reference member, so
// an OldPayGateway can still be assigned; it never deletes the terminal.
oldpay::Terminal* terminal_;
};

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

shapes.hpp
// The abstraction side of the bridge: what a shape is, in terms of the
// Canvas operations, with no knowledge of any output format.
// draw() is the bridge itself: a shape is handed a canvas and describes
// itself by calling line, circle, and text on it. The canvas is passed by
// reference (Canvas&), so the shape uses the caller's canvas, not a copy,
// and does not own it or keep it after draw returns. The copy operations
// are protected for the same reason as Canvas's: no slicing.
class Shape {
public:
virtual ~Shape() = default;
// Pure virtual (= 0): every shape must say how it is drawn. `const`:
// drawing does not change the shape. The canvas is not const, because
// drawing writes to it.
virtual void draw(Canvas& canvas) const = 0;
protected:
Shape() = default;
Shape(const Shape&) = default;
Shape& operator=(const Shape&) = default;
};

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

canvas.hpp
// The implementation side of the bridge: the few things every output
// format can do. Shapes are drawn using only these.
// Each function is `virtual ... = 0` ("pure virtual"): Canvas gives no
// body, so it cannot be created on its own, and every concrete canvas
// must write all three. A call through a Canvas& runs the version of
// whichever canvas the reference really refers to. A Point is a small
// struct of two ints, x and y, so it is passed by value. The functions
// return nothing: a canvas keeps what it is given, and the code that
// created it reads the result out afterwards.
class Canvas {
public:
// Virtual, so that destroying a canvas through a Canvas pointer also
// runs the derived class's destructor.
virtual ~Canvas() = default;
virtual void line(Point from, Point to) = 0;
virtual void circle(Point centre, int radius) = 0;
// std::string_view is a read-only view of characters owned by the
// caller: no copy is made. A canvas that wants to keep the words must
// copy them before the call returns.
virtual void text(Point at, std::string_view words) = 0;
protected:
// Protected: only derived classes can use these. So code holding two
// Canvas references cannot assign one to the other, which would copy
// only the Canvas part and not the derived data ("slicing").
Canvas() = default;
Canvas(const Canvas&) = default;
Canvas& operator=(const Canvas&) = default;
};

class OldPayGateway final

the adapter; derives from PaymentGateway

Data members

  • oldpay::Terminal* terminal_; borrowed: the adaptee, written by the vendor; must outlive this

Member functions

  • explicit OldPayGateway(oldpay::Terminal& terminal);
  • ChargeResult charge(const ChargeRequest& request) override; translates in, calls authorize, translates out
Adapter: one class that makes the vendor's Terminal fit the shop's PaymentGateway

interface (abstract class) Shape

the abstraction: what to draw

Member functions

  • virtual void draw(Canvas& canvas) const = 0; uses only Canvas operations

class Rectangle final

a refined abstraction; derives from Shape

Data members

  • Point top_left_;
  • int width_;
  • int height_;

Member functions

  • void draw(Canvas& canvas) const override; four calls to line

interface (abstract class) Canvas

the implementor: how to draw

Member functions

  • virtual void line(Point from, Point to) = 0;
  • virtual void circle(Point centre, int radius) = 0;
  • virtual void text(Point at, std::string_view words) = 0;

class SvgCanvas final

a concrete implementor; derives from Canvas

Data members

  • std::string elements_;

Member functions

  • void line(Point from, Point to) override; appends an SVG line element
  • std::string document() const;
Bridge: two hierarchies, each written against the other's interface
Adapter: one interface, one class per outside system. Bridge: two hierarchies that meet at one interface
Text description of the diagram

Two separate structures. First, the adapter: the shop’s PaymentGateway interface has one function, charge. OldPayGateway implements it, holds a pointer to the vendor’s Terminal, and translates each charge into a call to Terminal’s authorize function. Terminal is not part of the shop’s design and does not implement any shop interface. Second, the bridge: a Shape interface with one function, draw, which takes a Canvas; Rectangle and Badge implement it. A Canvas interface with line, circle, and text; SvgCanvas and TextCanvas implement it. Shapes depend on Canvas only, and canvases know nothing about shapes. Connector, the third shape, is left out to keep the diagram small.

Diagram source (Mermaid)
classDiagram
direction LR
class PaymentGateway {
  <<interface>>
  +charge(ChargeRequest) ChargeResult
}
class OldPayGateway {
  -Terminal* terminal_
  +charge(ChargeRequest) ChargeResult
}
class Terminal {
  +authorize(token, amount, currency) Response
}
PaymentGateway <|.. OldPayGateway
OldPayGateway --> Terminal : translates to
class Shape {
  <<interface>>
  +draw(Canvas&) void
}
class Rectangle
class Badge
class Canvas {
  <<interface>>
  +line(Point, Point) void
  +circle(Point, int) void
  +text(Point, string_view) void
}
class SvgCanvas
class TextCanvas
Shape <|.. Rectangle
Shape <|.. Badge
Canvas <|.. SvgCanvas
Canvas <|.. TextCanvas
Shape ..> Canvas : draws on
Question Adapter (OldPayGateway) Bridge (Shape and Canvas)
When is it designed? after both sides exist, when they turn out not to fit before the hierarchies grow, usually while the second side is still unwritten
Who designs the interface in the middle? the client: PaymentGateway says what the shop needs, in the shop’s terms the designer of both sides: Canvas is the few operations every format can do
Can the other side be changed? no: the vendor’s Terminal is fixed, so the adapter does the translating yes: every canvas is written to implement Canvas; any translation happens inside a canvas, not between the hierarchies
How many hierarchies vary? usually one: the target’s implementations, one adapter per vendor; the adaptee is not your hierarchy two, independently: shapes and formats
What does a new case cost? a new vendor: one new adapter a new shape: one class; a new format: one class; never one per pair
What does the class in the middle do? translates: units, codes, errors, exceptions nothing in between: shapes call canvases directly through the interface
  1. Did both sides exist before the interface? If one side was written by someone else, a vendor, another team, or a C library, and cannot be changed, the class that makes it fit is an adapter. The adaptee’s interface was never designed with yours in mind, which is why there is translation to do.
  2. Are there two things that vary, each with its own hierarchy? A bridge pays off when both sides vary: shapes and formats, messages and channels, reports and writers. If only one side has several classes, an interface with several implementations is enough; calling it a Bridge adds little.
  3. Is there translation in the middle? An adapter’s code is mostly conversion: cents to "12.34", Currency::eur to 978, vendor code 51 to ChargeStatus::declined, oldpay::LinkDown to try_again_later. In a bridge there is no mismatch to fix: a Rectangle calls canvas.line(...), and SvgCanvas::line turns that call into an SVG element. That is the work of producing a format, not of fitting one interface to another. If you find yourself translating between the two hierarchies of a bridge, the implementor interface was chosen badly.
  4. Which way does the design pressure point? An adapter keeps a foreign interface out of your code. A bridge keeps your two hierarchies apart from each other, so that neither multiplies the other.

This is the difference that matters most in practice.

  • Adapter: the target interface belongs to the client. PaymentGateway was written for the shop, in the shop’s types: a ChargeRequest in cents and a ChargeResult with a reason a customer can read. It does not mention the vendor at all, and it should not: when the shop adds a second vendor, it writes a second adapter against the same interface. The adaptee’s interface is fixed, and you take it as it is.
  • Bridge: the implementor interface belongs to the design. The designer chose Canvas by asking what every format can do, so that every shape can be drawn with it. It is deliberately small and primitive. Its quality decides whether the bridge works: if a new format cannot be written with line, circle, and text, the interface must change, and every canvas with it. The Bridge page’s When not to use it covers the case where no common operations exist.
  • An adapter holds its adaptee, borrowed or owned, and usually little else. OldPayGateway holds a oldpay::Terminal*, so the terminal must outlive the adapter; its constructor takes a non-const reference, which stops a temporary from being passed. the Adapter page explains why it borrows rather than owns. A class adapter, which inherits privately from the adaptee, owns it as a base-class subobject instead.
  • A bridge’s abstraction either receives its implementor per call or stores it. In the classic form the abstraction stores it, set at construction; this example passes it to each call instead (see the Bridge page’s pass-or-store trade-off). Shape::draw takes a Canvas& and keeps nothing, so there is no lifetime rule. In the stored form, each abstraction object holds a pointer or reference to its implementor and must not outlive it, or owns it in a std::unique_ptr. The Bridge page’s Ownership and lifetime section covers both.
  • Neither pattern requires shared ownership. If you reach for std::shared_ptr to keep the other side alive, first ask who actually owns it; usually the code that creates both objects does.

The two patterns are not exclusive, and real designs often contain both.

  • A concrete implementor can be an adapter. Suppose the shape program must also write PDF, through a PDF library the team did not write. PdfCanvas implements Canvas, which makes it part of the bridge, and does so by translating each line call into the library’s own calls and units, which makes it an adapter. The bridge decides where the line between the hierarchies is; the adapter is how one outside library is fitted to that line.
  • Several adapters behind one interface are still adapters. If the shop adds a second vendor, it has PaymentGateway with OldPayGateway and, say, NewPayGateway. That is one interface with two implementations. It becomes a bridge only if the other side also grows into a hierarchy designed against PaymentGateway, for example one-off orders and subscriptions, each built on the gateway’s operations.
  • PImpl looks like a bridge with one implementation. It separates a class from its private data for compile-time and binary-stability reasons, not to let two hierarchies vary. The Bridge page’s Bridge and the PImpl idiom section explains the difference.
  • You own both sides and they do not fit. Change one of them. An adapter between two classes the same team writes is usually a sign that one interface was not finished.
  • Only one thing varies. One interface with several implementations, such as a sort order passed to a function, is enough; calling it a Bridge adds a word, not a design. (PaymentGateway’s implementations are still adapters, because each translates, but there is no Bridge.)
  • The second side is imagined. A Canvas interface for a program that will only ever write SVG is unnecessary abstraction. Write the SVG code directly, and split it when the second format is real.

Exercise: Classify three designs

For each design, say whether it is an Adapter, a Bridge, both, or neither, using the questions above. Say what the interface in the middle is, and who should design it.

  1. A game calls Sound::play(SoundId id, int volume_percent) everywhere. It now has to use a third-party audio library whose only function is play_wav(const char* path, float volume), with volume from 0.0 to 1.0, and which returns an error code instead of throwing.
  2. A reporting tool, still being designed, will produce invoices, statements, and summaries, and must write each of them as PDF, HTML, and CSV. More reports and more formats are expected.
  3. An order service sends notifications through one Notifier interface. EmailNotifier implements it with a vendor’s SMTP library, and SmsNotifier with another vendor’s web API. The order service is the only caller.
Show the explained answer

1. An adapter. Both sides exist: the game’s Sound interface and the vendor’s play_wav (question 1). There is plenty to translate: SoundId to a file path, a percentage to a float, an error code to the game’s error handling (question 3). Only one side varies, if any. The target interface, Sound, belongs to the game and should not change to suit the library. The adapter has no adaptee object to hold, only free functions, so the question of borrowing or owning does not arise; it may own a table from SoundId to path.

2. A bridge. Two things vary independently, reports and formats, and without a split they would need nine classes, growing with every addition (question 2). It is being designed now, so both sides can be written to fit one interface. The designer chooses it: the few operations every format can do, such as heading, row, and total, which each report uses to describe itself. CSV will make that choice hard, since it has a header row of column names but no section headings; deciding what heading means for CSV, or whether CSV belongs in a different interface, is the real design work.

3. Adapters, not a bridge. EmailNotifier and SmsNotifier each fit a vendor library to the service’s own Notifier interface, so each is an adapter (questions 1 and 3), and Notifier should be written in the service’s terms, not either vendor’s. Only one side has a hierarchy: there is one order service. If the service later grew separate message classes, such as order shipped or password reset, that each build their text from Notifier operations, the design would have the second hierarchy that makes it a bridge, with the two notifiers as concrete implementors that are also adapters.