All Articles

Structural Design Pattern: Bridge

This article was done using my notes from:

Alexander Shvets (2019), Dive into Design Patterns, Refactoring.Guru


Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies — abstraction and implementation — which can be developed independently of each other.

The Abstraction provides high-level control logic. It relies on the implementation object to do the actual low-level work.

The Implementation declares the interface that’s common for all concrete implementations.

An abstraction can only communicate with an implementation object via methods that are declared here.


Example 1:


Example 2:



from __future__ import annotations
from abc import ABC, abstractmethod

class Abstraction:
    The Abstraction defines the interface for the "control" part of the two
    class hierarchies.

    def __init__(self, implementation: Implementation) -> None:
        self.implementation = implementation

    def operation(self) -> str:
        return (f"Abstraction: Base operation with:\n"

class Implementation(ABC):
    The Implementation defines the interface for all implementation classes.

    The Implementation interface provides only primitive operations, while the Abstraction defines higher-
    level operations based on those primitives.

    def operation_implementation(self) -> str:

Each Concrete Implementation corresponds to a specific platform and implements
the Implementation interface using that platform's API.

class ConcreteImplementationA(Implementation):
    def operation_implementation(self) -> str:
        return "ConcreteImplementationA: Here's the result on the platform A."

class ConcreteImplementationB(Implementation):
    def operation_implementation(self) -> str:
        return "ConcreteImplementationB: Here's the result on the platform B."