设计模式十九(中介者模式,python语言实现)
基本原理请参考相关资料,这里直接给实例。
基本说明:中介者模式,( 仲裁者、集中控制),隔离了各个对象间的直接耦合
指挥中心分:灾害处理中心(DisasterDealCenter),边境处理中心(FrontierDealCenter)
处理小分队分:警犬巡逻队(SentryA),边境情报对(SentryB),边境缉毒队(sentryC)
小队间是平行级别,需要帮助时,向注册的指挥中心发出请求。
指挥中心根据请求,回复该小队,并安排其它小队配合
# -*- coding: GB18030 -*-######################################################## # mediator.py# Python implementation of the Class DisasterDealCenter# Generated by Enterprise Architect# Created on: 15-十二月-2012 13:26:57# #######################################################from __future__ import divisionfrom __future__ import print_functionfrom __future__ import unicode_literalsfrom future_builtins import * class Headquarters(object): """This class defines an interface for communicating with Colleague objects. """ def __init__(self,name='司令部'): self.name=name pass def Request(self, sentry): passclass DisasterDealCenter(Headquarters): """This class defines an interface for communicating with Colleague objects. """ def __init__(self,name='灾害处理中心'): super(DisasterDealCenter,self).__init__(name) pass def Request(self, sentry): passclass FrontierDealCenter(Headquarters): """This class defines an interface for communicating with Colleague objects. """ def __init__( self,name='边防处理中心'): super(FrontierDealCenter,self).__init__(name) self.ls=list() pass def Register(self,sentry): self.ls.append(sentry) pass def Request(self, sentry): print('#############################') print(self.name+':') print(' 收到信息:'+sentry.info+ ' 来源:' +sentry.name) print(' 处理:请其它小队配合') print('\n') sentry.Recieve('已派其它小队配合你部') print('\n') for obj in self.ls: if obj!=sentry: obj.Recieve('请迅速配合'+sentry.name) pass pass passclass Sentry(object): """Each colleague class knows its mediator object and communicates with its mediator whenever it would have otherwise communicated with another colleague. """ m_Headquarters= Headquarters() def __init__(self, name): self.info=None self.mediator=None self.name=name pass def Recieve(self, info): print(self.name+':') print(' 收到信息:'+info) pass def Send(self,mediator): pass def Register(self,mediator): self.mediator=mediator pass class SentryA(Sentry): """Each colleague class knows its mediator object and communicates with its mediator whenever it would have otherwise communicated with another colleague. """ m_FrontierDealCenter= FrontierDealCenter() def __init__(self, name='警犬巡逻队'): super(SentryA,self).__init__(name) pass def Send(self): self.info='发现可疑物品' self.mediator.Request(self) passclass SentryB(Sentry): """Each colleague class knows its mediator object and communicates with its mediator whenever it would have otherwise communicated with another colleague. """ m_FrontierDealCenter= FrontierDealCenter() def __init__(self, name='边境情报队'): super(SentryB,self).__init__(name) def Send(self): self.info='四点整,边境将有可疑组织活动' self.mediator.Request(self) passclass SentryC(Sentry): """Each colleague class knows its mediator object and communicates with its mediator whenever it would have otherwise communicated with another colleague. """ m_FrontierDealCenter= FrontierDealCenter() def __init__(self, name='边境缉毒队'): super(SentryC,self).__init__(name) def Send(self): self.info='有毒贩入境' self.mediator.Request(self) pass #客户端if(__name__=="__main__"): #建立边防事务指挥中心 m_FrontierDealCenter=FrontierDealCenter() #成立边防小队 m_SentryA=SentryA() m_SentryB=SentryB() m_SentryC=SentryC() #在指挥中心注册小队 m_FrontierDealCenter.Register(m_SentryA) m_FrontierDealCenter.Register(m_SentryB) m_FrontierDealCenter.Register(m_SentryC) #在各小队注册指挥中心 m_SentryA.Register(m_FrontierDealCenter) m_SentryB.Register(m_FrontierDealCenter) m_SentryC.Register(m_FrontierDealCenter) #请求 m_SentryB.Send() m_SentryA.Send()
运行结果: