Bridge Pattern in Python
Split what something is from how it is delivered, so you can combine any message type with any channel.
- Two dimensions that vary
- Composition instead of subclass explosion
- Swapping the implementation at run time
- Alert
- Reminder
- sender
- 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.
Alert🎮 The remote: what you want to doEmailSender📺 The device: how it is doneself.sender = sender🔗 The remote is connected to a deviceself.sender.send(text)📡 The remote forwards the commandThe 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
Alertis the abstraction: it decides when to notify.EmailSenderis the implementation: it knows how to deliver text.self.sender = senderstores the implementation inside the abstraction; this link is the bridge.notifyforwards toself.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
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"))
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.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Creating a subclass per combination
Every new channel or message type multiplies the number of classes.
class EmailAlert(Alert): ...
class SmsAlert(Alert): ...
Alert(EmailSender())
Alert(SmsSender())
Finished this lesson?
Mark it as learned to track your progress.