Bridge Pattern in Python

Sean Bradley
Design Patterns In Python
5 min readApr 18, 2022

--

The Bridge pattern is similar to the Adapter pattern except in the intent that you developed it.

The Bridge is an approach to refactor already existing code, whereas the Adapter creates an interface on top of existing code through existing available means without refactoring any existing code or interfaces.

The motivation for converting your code to the Bridge pattern is that it may be tightly coupled. There is logic and abstraction close together that is limiting your choices in how you can extend your solution in the way that you need.

E.g., you may have one Car class, that produces a very nice car. But you would like the option of varying the design a little, or outsourcing responsibility of creating the different components.

The Bridge pattern is a process about separating abstraction and implementation, so this will give you plenty of new ways of using your classes.

CAR = Car()
print(CAR)
> Car has wheels and engine and windows and everything else.

But you would like to delegate the engine dynamically from a separate set of classes or solutions.

ENGINE = EngineA()
CAR = Car(EngineA)

A Bridge didn’t exist before, but since after the separation of interface and logic, each side can be extended independently of each other.

Also, the application of a Bridge in your code should use composition instead of inheritance. This means that you assign the relationship at…

--

--

Sean Bradley
Design Patterns In Python

Developer of real time, low latency, high availability, asynchronous, multi threaded, remotely managed, fully automated and monitored solutions.