The before code tightly couples dialog classes with specific button implementations, causing class explosion. The after code separates the abstraction (Dialog) from the implementation (ButtonImplementation), allowing independent extension and reuse.
### Before Bridge Pattern (tight coupling, many classes)
class WindowsButton:
def draw(self):
print("Drawing button in Windows style")
class MacOSButton:
def draw(self):
print("Drawing button in MacOS style")
class WindowsDialog:
def render(self):
button = WindowsButton()
button.draw()
class MacOSDialog:
def render(self):
button = MacOSButton()
button.draw()
### After Bridge Pattern (decoupled abstraction and implementation)
class ButtonImplementation:
def draw_button(self):
pass
class WindowsButtonImpl(ButtonImplementation):
def draw_button(self):
print("Drawing button in Windows style")
class MacOSButtonImpl(ButtonImplementation):
def draw_button(self):
print("Drawing button in MacOS style")
class Dialog:
def __init__(self, button_impl: ButtonImplementation):
self.button_impl = button_impl
def render(self):
self.button_impl.draw_button()
# Usage
windows_dialog = Dialog(WindowsButtonImpl())
windows_dialog.render() # Drawing button in Windows style
mac_dialog = Dialog(MacOSButtonImpl())
mac_dialog.render() # Drawing button in MacOS style