標籤: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: " + msgclass 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: " + msgclass 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" )
中介者模式 python