ArticleslgStudy

computer science

Bridge pattern

Bridge pattern is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bridge pattern rather than just read about it. In short: The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently", introduced by the Gang of Four. The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.

Bridge pattern — main illustration
Bridge pattern — illustration

Key takeaways

  • Bridge pattern belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bridge pattern to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bridge pattern from memory before moving on to harder problems.

Reference excerpt

The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently", introduced by the Gang of Four. The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes. When a class varies often, the features of object-oriented programming become very useful because changes to a program's code can be made easily with minimal prior knowledge about the program. The bridge pattern is useful when both the class and what it does vary often. The class itself can be thought of as the abstraction and what the class can do as the implementation. The bridge pattern can also be thought of as two layers of abstraction. When there is only one fixed implementation, this pattern is known as the pointer-to-implementation idiom (or PIMPL idiom) in C++ contexts; the "pointer" here refers to an opaque pointer. Other names are "handle classes" and "compiler firewall idiom"; in Qt and KDE, they are known as the "d-pointer" and "Cheshire Cat" sometimes. The bridge pattern is often confused with the adapter pattern, and is often implemented using the object adapter pattern. Variant: The implementation can be decoupled even more by deferring the presence of the implementation to the point where the abstraction is utilized.

Overview The Bridge design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. The bridge pattern solves these problems:

An abstraction and its implementation should be defined and extended independently from each other. A compile-time binding between an abstraction and its implementation should be avoided so that an implementation can be selected at run-time. When using inheritance, different subclasses implement an abstract class in different ways. But an implementation is bound to the abstraction at compile-time and cannot be changed at run-time. The Bridge design pattern describes the following solutions:

Separate an abstraction (Abstraction) from its implementation (Implementor) by putting them in separate class hierarchies. Implement the Abstraction in terms of (by delegating to) an Implementor object. This enables to configure an Abstraction with an Implementor object at run-time.

See also the Unified Modeling Language class and sequence diagram below.

PIMPL idiom Traditionally, the PIMPL idiom was used in C++ to decouple the class's API from its implementation, as well as guarantee ABI stability. It guarantees ABI stability by hiding the data behind a fixed-size pointer (an opaque pointer), allowing for changes to be made to a class's implementation without breaking binary compatibility. Additionally, private data can be completely made absent from client-facing code, providing stronger encapsulation. The PIMPL idiom traditionally increases compilation speed by limiting cascading recompilations when a private member changes; additionally, with header files, dependencies can be prevented from leaking into clients via separate compilation; this also allows consumers only to compile large dependencies once (in the implementation file), rather than multiple times per inclusion. However, since C++20, modules achieve the same compilation speed improvements of the PIMPL idiom as a module is compiled only once, unlike a header. However, using the PIMPL idiom has a small indirection cost, as each data access must go through a pointer dereference, and the objects reside on the heap.

Structure

UML class and sequence diagram

In the above Unified Modeling Language class diagram, an abstraction (Abstraction) is not implemented as usual in a single inheritance hierarchy. Instead, there is one hierarchy for an abstraction (Abstraction) and a separate hierarchy for its implementation (Implementor), which makes the two independent from each other. The Abstraction interface (operation()) is implemented in terms of (by delegating to) the Implementor interface (imp.operationImp()).

The UML sequence diagram shows the run-time interactions: The Abstraction1 object delegates implementation to the Implementor1 object (by calling operationImp() on Implementor1), which performs the operation and returns to Abstraction1.

Class diagram

Abstraction (abstract class) defines the abstract interface maintains the Implementor reference. RefinedAbstraction (normal class) extends the interface defined by Abstraction Implementor (interface) defines the interface for implementation classes ConcreteImplementor (normal class) implements the Implementor interface

Example

Crystal

Output

C# Bridge pattern compose objects in tree structure. It decouples abstraction from implementation. Here abstraction represents the client from which the objects will be called. An example implemented in C# is given below:

The Bridge classes are the Implementation that uses the same interface-oriented architecture to create objects. On the other hand, the abstraction takes an instance of the implementation class and runs its method. Thus, they are completely decoupled from one another.

C++

Bridge pattern

Output:

PIMPL idiom This example demonstrates the PIMPL idiom, with an internal Widget::Impl class held by a std::unique_ptr. Even using modules, the class declaration and implementation remain in separate files (the "module interface unit" and the "module implementation unit"). The module interface unit, Foo.cppm:

The module interface unit, Foo.cpp:

Java The following Java program defines a bank account that separates the account operations from the logging of these operations.

It will output:

PHP

Output:

Scala

Python

See also Adapter pattern Strategy pattern Template method pattern

References

External links

Bridge in UML and in LePUS3 (a formal modelling language) C# Design Patterns: The Bridge Pattern From: James W. Cooper (2003). C# Design Patterns: A Tutorial. Addison-Wesley. ISBN 0-201-84453-2.

Illustrations

Bridge pattern illustration
Bridge pattern: Bridge in LePUS3 (legend)
Bridge in LePUS3 (legend)

Worked examples

Example 1 — a first encounter with Bridge pattern

Start with the simplest possible case. Write down what Bridge pattern claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bridge pattern before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bridge pattern ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bridge pattern

In research
Bridge pattern appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bridge pattern in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bridge pattern is common in secondary-school and first-year university syllabi. It links to neighbouring topics Software design patterns, so understanding it makes those chapters shorter.
In everyday life
Look for Bridge pattern outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bridge pattern” →

How to study Bridge pattern in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bridge pattern means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bridge pattern out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bridge pattern in simple terms?

The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently", introduced by the Gang of Four. The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibili…

Why does Bridge pattern matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bridge pattern?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bridge pattern.

Tags

  • Software design patterns

Keep exploring