Intermediary mode (Python)

Source: Internet
Author: User

Intermediary Mode: Encapsulate a series of object interactions with an intermediary object. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.
AverageApplicationWhen a group of objects communicate in a well-defined but complex manner.
Advantages: Reduces the coupling of each module.

Disadvantages: The intermediary objects are easy to become complex and huge.

# Encoding = UTF-8 ## by Panda # intermediary mode def printinfo (Info): Print Unicode (Info, 'utf-8 '). encode ('gbk') # Abstract intermediary class mediator (): def send (self, message, colleague): pass # Abstract colleague class colleague (): mediator = none def _ init _ (self, mediator): Self. mediator = mediator # A colleague class concretecolleague (colleague): name = ''def _ init _ (self, name, mediator): Self. name = Name colleags. _ init _ (self, mediator) def send (self, message): Self. mediator. send (message, self) def every Y (self, message): printinfo ('% s) Get the recipient's message: % s' % (self. name, message) # The mediator class concretemediator (mediator): name = ''colleague1 = none colleague2 = none def _ init _ (self, name): Self. name = Name def send (self, message, colleague): If colleague = self. colleague1: Self. colleague2.notify (Message) else: Self. colleague1.y y (Message) def clientui (): mediator = concretemediator ('United Nations ') USA = concretecolleague ('America', mediator) mediator. colleague1 = USA Iraq = concretecollegou ('Iraqi ', mediator) mediator. colleague2 = Iraq USA. send ('nuclear weapons are not allowed to be developed, or war should be launched ') Iraq. send ('we do not have nuclear weapons and are not afraid of aggression ') returnif _ name _ =' _ main _ ': clientui ();

Class Diagram:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.