|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | + |
| 4 | +# -------------------- Mediator Interface -------------------- |
| 5 | +class Mediator(ABC): |
| 6 | + @abstractmethod |
| 7 | + def notify(self, sender: object, event: str): |
| 8 | + pass |
| 9 | + |
| 10 | + |
| 11 | +# -------------------- Base Component -------------------- |
| 12 | +class Component: |
| 13 | + def __init__(self, mediator: Mediator = None): |
| 14 | + self._mediator = mediator |
| 15 | + |
| 16 | + def set_mediator(self, mediator: Mediator): |
| 17 | + self._mediator = mediator |
| 18 | + |
| 19 | + |
| 20 | +# -------------------- Concrete Components -------------------- |
| 21 | +class Button(Component): |
| 22 | + def click(self): |
| 23 | + print("[Button Clicked]") |
| 24 | + self._mediator.notify(self, "click") |
| 25 | + |
| 26 | + |
| 27 | +class Checkbox(Component): |
| 28 | + def __init__(self, mediator=None): |
| 29 | + super().__init__(mediator) |
| 30 | + self.checked = False |
| 31 | + |
| 32 | + def toggle(self): |
| 33 | + self.checked = not self.checked |
| 34 | + print(f"[Checkbox Toggled] checked = {self.checked}") |
| 35 | + self._mediator.notify(self, "check") |
| 36 | + |
| 37 | + |
| 38 | +class Textbox(Component): |
| 39 | + def __init__(self, mediator=None): |
| 40 | + super().__init__(mediator) |
| 41 | + self.visible = True |
| 42 | + self.content = "" |
| 43 | + |
| 44 | + def set_visible(self, visible: bool): |
| 45 | + self.visible = visible |
| 46 | + print(f"[Textbox] {'Shown' if visible else 'Hidden'}") |
| 47 | + |
| 48 | + def set_content(self, content: str): |
| 49 | + self.content = content |
| 50 | + |
| 51 | + |
| 52 | +# -------------------- Concrete Mediator -------------------- |
| 53 | +class AuthenticationDialog(Mediator): |
| 54 | + def __init__(self): |
| 55 | + # Components |
| 56 | + self.title = "Dialog" |
| 57 | + self.login_checkbox = Checkbox(self) |
| 58 | + self.username_login = Textbox(self) |
| 59 | + self.password_login = Textbox(self) |
| 60 | + |
| 61 | + self.username_register = Textbox(self) |
| 62 | + self.password_register = Textbox(self) |
| 63 | + self.email_register = Textbox(self) |
| 64 | + |
| 65 | + self.ok_button = Button(self) |
| 66 | + self.cancel_button = Button(self) |
| 67 | + |
| 68 | + self.set_initial_state() |
| 69 | + |
| 70 | + def set_initial_state(self): |
| 71 | + self.title = "Login" |
| 72 | + self.username_login.set_visible(True) |
| 73 | + self.password_login.set_visible(True) |
| 74 | + |
| 75 | + self.username_register.set_visible(False) |
| 76 | + self.password_register.set_visible(False) |
| 77 | + self.email_register.set_visible(False) |
| 78 | + |
| 79 | + def notify(self, sender: object, event: str): |
| 80 | + if sender == self.login_checkbox and event == "check": |
| 81 | + if self.login_checkbox.checked: |
| 82 | + self.title = "Login" |
| 83 | + print("\n[Dialog switched to Login mode]") |
| 84 | + self.username_login.set_visible(True) |
| 85 | + self.password_login.set_visible(True) |
| 86 | + self.username_register.set_visible(False) |
| 87 | + self.password_register.set_visible(False) |
| 88 | + self.email_register.set_visible(False) |
| 89 | + else: |
| 90 | + self.title = "Register" |
| 91 | + print("\n[Dialog switched to Registration mode]") |
| 92 | + self.username_login.set_visible(False) |
| 93 | + self.password_login.set_visible(False) |
| 94 | + self.username_register.set_visible(True) |
| 95 | + self.password_register.set_visible(True) |
| 96 | + self.email_register.set_visible(True) |
| 97 | + |
| 98 | + elif sender == self.ok_button and event == "click": |
| 99 | + if self.login_checkbox.checked: |
| 100 | + print("\n[Logging in user...]") |
| 101 | + # In real code, validate login form |
| 102 | + if not self.username_login.content or not self.password_login.content: |
| 103 | + print("Login failed: Missing username or password.") |
| 104 | + else: |
| 105 | + print(f"Login success for user: {self.username_login.content}") |
| 106 | + else: |
| 107 | + print("\n[Registering new user...]") |
| 108 | + # In real code, validate registration form |
| 109 | + if not self.username_register.content or not self.password_register.content or not self.email_register.content: |
| 110 | + print("Registration failed: Missing fields.") |
| 111 | + else: |
| 112 | + print(f"User {self.username_register.content} registered with email {self.email_register.content}.") |
| 113 | + |
| 114 | + |
| 115 | +# -------------------- Application (Client Code) -------------------- |
| 116 | +def main(): |
| 117 | + dialog = AuthenticationDialog() |
| 118 | + |
| 119 | + # Toggle between login and register |
| 120 | + dialog.login_checkbox.toggle() # Switch to Login |
| 121 | + dialog.username_login.set_content("john_doe") |
| 122 | + dialog.password_login.set_content("123456") |
| 123 | + dialog.ok_button.click() |
| 124 | + |
| 125 | + print("\n---\n") |
| 126 | + |
| 127 | + dialog.login_checkbox.toggle() # Switch to Register |
| 128 | + dialog.username_register.set_content("jane_doe") |
| 129 | + dialog.password_register.set_content("abcdef") |
| 130 | + dialog.email_register.set_content("jane@example.com") |
| 131 | + dialog.ok_button.click() |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + main() |
0 commit comments