中介者模式 python
class Infantry( object ): def __init__( self, mediator, name ): self.name = name self.mediator = mediator def send( self, msg ): self.mediator.send( msg, self ) def notify( self, msg ): print self.name + " get infomation: " + msg class AirForce( object ): def __init__( self, mediator, name ): self.name = name self.mediator = mediator def send( self, msg ): self.mediator.send( msg, self ) def notify( self, msg ): print self.name + " get infomation: " + msg class Mediator( object ): soliders = [] def send( self, msg, solider ): for s in self.soliders: if s != solider: s.notify( msg ) def register( self, solider ): self.soliders.append( solider ) m = Mediator() t1 = Infantry( m, "Infantry-1" ) t2 = AirForce( m, "AirForce-1" ) t3 = AirForce( m, "AirForce-2" ) m.register( t1 ) m.register( t2 ) m.register( t3 ) t1.send( "I have found killer" ) t2.send( "Look at him" ) t3.send( "Be careful" )
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。