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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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).