class Mediator: def __init__(self): pass class CarMediator(Mediator): def setObj(self, Buyller, Seller): self.buyller = Buyller self.seller = Seller def buy(self): print('buyller buy car') self.seller.giveMoney() def sell(self): print('seller seller car') self.buyller.getMoney() class Colleague: def __init__(self, mediator): self.mediator = mediator class Buyller(Colleague): def buy(self): self.mediator.sell() def getMoney(self): print('获取钱') class Seller(Colleague): def sell(self): self.mediator.buy() def giveMoney(self): print('give money') if __name__ == '__main__': mediator = CarMediator() buyller = Buyller(mediator) seller = Seller(mediator) mediator.setObj(buyller, seller) buyller.buy() seller.sell()