Python डिज़ाइन पैटर्न

Mediator Pattern in Python

Mediator Pattern in Python को संपादित और ऑनलाइन चलाए जा सकने वाले व्यावहारिक उदाहरण से सीखें।

Mediator Pattern क्या है?

Mediator Pattern in Python को संपादित और ऑनलाइन चलाए जा सकने वाले व्यावहारिक उदाहरण से सीखें।

इसका उपयोग कब करें?

  • सिद्ध संरचनाओं से बार-बार आने वाली object-design समस्याएँ हल करें।
  • जिम्मेदारियों को अलग करें ताकि code को बढ़ाना और test करना आसान हो।
  • टीम में architecture पर चर्चा करते समय साझा design शब्दावली का उपयोग करें।

उदाहरण कोड

कोड चलाएँ →
main.py
class ChatRoom:
    # Mediator: route colleague communication through a coordinator.
    def send(self, sender, recipient, message):
        # Coordinate communication between colleague objects.
        recipient.receive(sender.name, message)


class User:
    def __init__(self, name, room): self.name, self.room = name, room
    def send(self, recipient, message): self.room.send(self, recipient, message)
    def receive(self, sender, message): print(f"{self.name} from {sender}: {message}")


room = ChatRoom()
ada, lin = User("Ada", room), User("Lin", room)
ada.send(lin, "Hello")

अपेक्षित आउटपुट

Lin from Ada: Hello

यह कैसे काम करता है

यह उदाहरण Mediator Pattern in Python दिखाता है। कोड समझने के लिए इनपुट मान बदलें।

मान बदलें और Python इंस्टॉल किए बिना CodeUtility ऑनलाइन Python कंपाइलर में प्रोग्राम चलाएँ।

अभ्यास के कार्य

बड़े dataset पर जाने से पहले input बदलें और edge cases की जाँच करें।

  1. Pattern की संरचना बनाए रखते हुए उदाहरण का domain बदलें।
  2. Pattern की तुलना सरल implementation से करें और उनके trade-off समझाएँ।
  3. नई implementation जोड़ने से पहले प्रत्येक participant के लिए test लिखें।
Python IDE में चलाएँ →