Python Design Patterns

Bridge Pattern in Python

Split what something is from how it is delivered, so you can combine any message type with any channel.

  • Advanced
  • 2 min read
  • 1 runnable example
What you'll learn
  • Two dimensions that vary
  • Composition instead of subclass explosion
  • Swapping the implementation at run time
Alert(EmailSender())
Abstraction
  • Alert
  • Reminder
holds →
  • sender
Implementation
  • EmailSender
  • SmsSender

Abstraction holds an implementation

The problem

Imagine alerts and reports that can each be sent by email, SMS or chat. With inheritance you end up with a class for every combination: EmailAlert, SmsAlert, EmailReport... The number of classes multiplies. A bridge separates the two dimensions.

Think of it like this

A bridge is like a TV remote and the TV. Remotes (simple, universal, voice) and TVs (brand A, brand B) can be developed separately. Any remote can drive any TV, because the remote holds a connection to a TV and sends it commands.

From a remote and a TV to code
Alert🎮 The remote: what you want to do
EmailSender📺 The device: how it is done
self.sender = sender🔗 The remote is connected to a device
self.sender.send(text)📡 The remote forwards the command
Two sides that vary independently, joined by composition.
In one sentence

The Bridge pattern splits an abstraction (what to do) from its implementation (how to do it) and connects them by composition, so both can vary independently.

Key points

  1. Alert is the abstraction: it decides when to notify.
  2. EmailSender is the implementation: it knows how to deliver text.
  3. self.sender = sender stores the implementation inside the abstraction; this link is the bridge.
  4. notify forwards to self.sender.send, so an SMS sender could be plugged in without a new Alert class.

When should you use it?

  • Two independent dimensions, such as message type and delivery channel.
  • Avoiding a class for every combination of features.
  • Switching implementations at run time.

When not to use it

  • Only one dimension varies: a single class hierarchy or a strategy is enough.
  • Small programs where the extra indirection makes the code harder to follow.

Example code

Run code →
main.py
class EmailSender:
    # Bridge: vary the abstraction and implementation independently.
    def send(self, text): return f"Email: {text}"


class Alert:
    def __init__(self, sender):
        # Composition forms the bridge to an implementation.
        self.sender = sender

    def notify(self, text):
        return self.sender.send(text)


print(Alert(EmailSender()).notify("Alert"))
Get a Run button for this example

Result of running the code

Email: Alert

Code explanation

Alert does not send anything itself; it keeps a sender object passed in when it is created. notify calls self.sender.send(text), so the delivery details stay in EmailSender. Creating Alert(EmailSender()) connects the two sides, and a different sender class could be plugged in without changing Alert.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Replace the sample domain while preserving the pattern structure.
  2. Compare the pattern with a simpler implementation and explain the tradeoff.
  3. Write tests for each participant before adding another implementation.
Run in the Python compiler →

Common mistakes

Creating a subclass per combination

Every new channel or message type multiplies the number of classes.

Problem
class EmailAlert(Alert): ...
class SmsAlert(Alert): ...
Fix
Alert(EmailSender())
Alert(SmsSender())

Finished this lesson?

Mark it as learned to track your progress.