Skip to content

Bridge pattern

Quality Score

Overall Score: 9.4/10 ⭐ Outstanding

  • Technical Accuracy: 32/35
  • Code Quality: 25/25
  • Educational Value: 24/25
  • Documentation: 13/15

Last reviewed: August 18, 2026

The bridge pattern is a structural design pattern that decouples an abstraction from its implementation, allowing the two to vary independently. It is useful when you want to avoid a permanent binding between an abstraction and its implementation, enabling flexibility and scalability in your code. The benefits are:

  • Decoupling: The bridge pattern separates the abstraction from its implementation, allowing them to evolve independently. This makes it easier to modify or extend either the abstraction or the implementation without affecting the other.
  • Flexibility: By using the bridge pattern, you can easily switch between different implementations of an abstraction at runtime. This is particularly useful when you have multiple variations of an abstraction that need to be combined with different implementations.
  • Scalability: The bridge pattern promotes scalability by allowing you to add new abstractions and implementations independently, without affecting existing code. This makes it easier to grow and maintain your system over time.

The beauty of the bridge pattern is that it is inherent in Python, making it easy to implement. It has two main components

Abstraction

The interface that defines the abstraction and maintains a reference to an object of the implementation hierarchy. The abstraction can be an abstract class or an interface that defines the common behavior for the abstraction, even can be extended without modifying the implementation.

src.advanced.bridge_pattern.Mode

Bases: ABC

Abstract mode interface.

Source code in src/advanced/bridge_pattern/bridge.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Mode(ABC):
    """Abstract mode interface."""

    @abstractmethod
    def get_background_color(self) -> str:
        """Get the background color for the mode.

        Returns:
            str: The background color as a string.
        """
        pass

    @abstractmethod
    def get_text_color(self) -> str:
        """Get the text color for the mode.

        Returns:
            str: The text color as a string.
        """
        pass

    @abstractmethod
    def get_highlight_color(self) -> str:
        """Get the highlight color for the mode.

        Returns:
            str: The highlight color as a string.
        """
        pass

    @abstractmethod
    def get_brightness(self) -> float:
        """Get the brightness factor for the mode (0.0 to 1.0).

        Returns:
            float: The brightness factor as a float.
        """
        pass

src.advanced.bridge_pattern.Element

Bases: ABC

Abstract element interface.

Element-intrinsic attributes (position, height_px) describe what the element is and are independent from the mode. The mode-driven palette and brightness are applied by apply_mode and stay orthogonal to the element's shape, showing the Bridge pattern.

Source code in src/advanced/bridge_pattern/bridge.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
class Element(ABC):
    """Abstract element interface.

    Element-intrinsic attributes (position, height_px) describe *what* the
    element is and are independent from the mode. The mode-driven palette and
    brightness are applied by apply_mode and stay orthogonal to the element's
    shape, showing the Bridge pattern.
    """

    position: str = ""
    height_px: int = 0

    def __init__(self, mode: Mode) -> None:
        """Initialize the element with a mode.

        Args:
            mode (Mode): The mode to apply to the element.
        """
        self.mode = mode
        self.apply_mode()

    def apply_mode(self) -> None:
        """Apply the mode's palette and brightness to the element.

        Sets the background color, text color, highlight color, and brightness
        attributes based on the current mode.
        """
        self.background_color = self.mode.get_background_color()
        self.text_color = self.mode.get_text_color()
        self.highlight_color = self.mode.get_highlight_color()
        self.brightness = self.mode.get_brightness()

    @abstractmethod
    def render(self) -> str:
        """Render the element as an HTML-like string using its current style.

        Returns:
            str: The HTML-like string representation of the element.
        """
        pass

Implementation

Implementation defines the interface for the implementation classes. It is responsible for providing the concrete implementation of the abstraction's behavior. New implementations can be added without changing the abstraction and without much effort. Also, implementations can be extended independently without affecting the others.

src.advanced.bridge_pattern.LightMode

Bases: Mode

Concrete light mode implementation.

Source code in src/advanced/bridge_pattern/bridge.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class LightMode(Mode):
    """Concrete light mode implementation."""

    def get_background_color(self) -> str:
        """Get the background color for the mode.

        Returns:
            str: The background color as a string.
        """
        return "white"

    def get_text_color(self) -> str:
        """Get the text color for the mode.

        Returns:
            str: The text color as a string.
        """
        return "black"

    def get_highlight_color(self) -> str:
        """Get the highlight color for the mode.

        Returns:
            str: The highlight color as a string.
        """
        return "lightgray"

    def get_brightness(self) -> float:
        """Get the brightness factor for the mode.

        Returns:
            float: The brightness factor as a float.
        """
        return 1.0

src.advanced.bridge_pattern.DarkMode

Bases: Mode

Concrete dark mode implementation.

Source code in src/advanced/bridge_pattern/bridge.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class DarkMode(Mode):
    """Concrete dark mode implementation."""

    def get_background_color(self) -> str:
        """Get the background color for the mode.

        Returns:
            str: The background color as a string.
        """
        return "black"

    def get_text_color(self) -> str:
        """Get the text color for the mode.

        Returns:
            str: The text color as a string.
        """
        return "white"

    def get_highlight_color(self) -> str:
        """Get the highlight color for the mode.

        Returns:
            str: The highlight color as a string.
        """
        return "darkgray"

    def get_brightness(self) -> float:
        """Get the brightness factor for the mode.

        Returns:
            float: The brightness factor as a float.
        """
        return 0.5

src.advanced.bridge_pattern.ColorBlindMode

Bases: Mode

Concrete color blind mode implementation.

Source code in src/advanced/bridge_pattern/bridge.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class ColorBlindMode(Mode):
    """Concrete color blind mode implementation."""

    def get_background_color(self) -> str:
        """Get the background color for the mode.

        Returns:
            str: The background color as a string.
        """
        return "lightyellow"

    def get_text_color(self) -> str:
        """Get the text color for the mode.

        Returns:
            str: The text color as a string.
        """
        return "darkblue"

    def get_highlight_color(self) -> str:
        """Get the highlight color for the mode.

        Returns:
            str: The highlight color as a string.
        """
        return "orange"

    def get_brightness(self) -> float:
        """Get the brightness factor for the mode.

        Returns:
            float: The brightness factor as a float.
        """
        return 1.0

src.advanced.bridge_pattern.Navbar

Bases: Element

Concrete navigation bar element.

Sits at the top of the page with a bottom shadow tinted by the mode's highlight color.

Source code in src/advanced/bridge_pattern/bridge.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
class Navbar(Element):
    """Concrete navigation bar element.

    Sits at the top of the page with a bottom shadow tinted by the mode's
    highlight color.
    """

    position: str = "top"
    height_px: int = 64

    def render(self) -> str:
        """Render the navigation bar as an HTML-like string.

        Returns:
            str: The HTML-like string representation of the element.
        """
        return (
            f"<nav style='position:{self.position}; "
            f"height:{self.height_px}px; "
            f"background:{self.background_color}; "
            f"color:{self.text_color}; "
            f"box-shadow:0 2px 4px {self.highlight_color}; "
            f"opacity:{self.brightness}'>Navbar</nav>"
        )

src.advanced.bridge_pattern.Footer

Bases: Element

Concrete footer element.

Sits at the bottom of the page with a top border tinted by the mode's highlight color.

Source code in src/advanced/bridge_pattern/bridge.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
class Footer(Element):
    """Concrete footer element.

    Sits at the bottom of the page with a top border tinted by the mode's
    highlight color.
    """

    position: str = "bottom"
    height_px: int = 40

    def render(self) -> str:
        """Render the footer as an HTML-like string.

        Returns:
            str: The HTML-like string representation of the element.
        """
        return (
            f"<footer style='position:{self.position}; "
            f"height:{self.height_px}px; "
            f"background:{self.background_color}; "
            f"color:{self.text_color}; "
            f"border-top:1px solid {self.highlight_color}; "
            f"opacity:{self.brightness}'>Footer</footer>"
        )

The common usage of the bridge pattern is often used in GUI frameworks, where you have different types of UI elements (abstractions) that can be rendered using different rendering engines (implementations).