代理模式
應用特性:需要在通訊雙方中間需要一些特殊的中間操作時引用,多加一個中間控制層。
結構特性:建立一個中間類,建立一個對象,接收一個對象,然後把兩者聯通起來
UML:
#!/usr/bin/env python #encoding: utf-8 class sender_base: def __init__(self): pass def send_something(self, something): pass class send_class(sender_base): def __init__(self, receiver): self.receiver = receiver def send_something(self, something): print "SEND " + something + ' TO ' + self.receiver.nameclass agent_class(sender_base): def __init__(self, receiver): self.send_obj = send_class(receiver) def send_something(self, something): self.send_obj.send_something(something) class receive_class: def __init__(self, someone): self.name = someone if '__main__' == __name__: receiver = receive_class('xiaohua') agent = agent_class(receiver) agent.send_something('agentinfo') print receiver.__class__ print agent.__class__
結果:
SEND agentinfo TO xiaohua__main__.receive_class__main__.agent_class