Python Design Patterns

Bridge Pattern in Python

Bridge prevents multiplying subclasses when two dimensions of behavior can change separately.

What is Bridge Pattern?

Bridge prevents multiplying subclasses when two dimensions of behavior can change separately.

Separate an abstraction from its implementation so both can vary independently.

When should you use it?

  • Solve recurring object-design problems with proven structures.
  • Decouple responsibilities so code is easier to extend and test.
  • Use shared design vocabulary when discussing architecture with a team.

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

Expected output

Email: Alert

How it works

Alert controls the high-level action while the sender object supplies the delivery mechanism.

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 Python IDE →