The Bridge Design Pattern with Python
Introduction
The Bridge Design Pattern is a Structural Design Pattern, which splits the abstraction from the implementation. In this article, we’ll be covering the motivation and implementation of the Bridge Design Pattern in Python.
Design Patterns refer to a set of standardized practices or solutions to common architectural problems in software engineering.
Motivation Behind the Bridge Design Pattern
The Bridge Pattern prevents what’s called the cartesian product complexity explosion.
The problem will be obvious going through an example. Suppose you’re implementing an Airplane
. It can be a military or commercial airplane. Furthermore, it can be a passenger/soldier or cargo airplane.
One approach to implementing this is by having a MilitaryPassenger
, MilitaryCargo
, CommercialPassenger
and CommercialCargo
airplanes.
Here the cartesian product complexity is 2 x 2 = 4
. This number isn’t ground-breaking at this scale, but when you include more classes and variations, it can rise exponentially and it’ll very quickly become unmanageable.
The Bridge Pattern is used, well, as a bridge between classes (Airplane
implementation) and their characteristics (is it a passenger or cargo plane). It favors composition over inheritance.
Using the pattern, you create one class for