Proxy pattern: Provides a proxy for other objects to control access to this object
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 __author__='Andy'5 6 """7 Liar design Mode8 design mode--proxy mode9 Proxy Pattern: Provides a proxy for other objects to control access to this objectTen realization: Dai for Cho Jayi gift to Jiao Jiao One Model: A #公共接口类 - class InterFace: - def request (self): the Pass - #真实类 - class Realsubject (InterFace): - def request (self): + print ' Realsubject request ' - #代理类 Call the inner method of the real class + class Proxysubject (InterFace) A def request (self): at self.real = Realsubject () - self.real.request () - """ - - - #Send Gift Interface in classGivegift (object): - #Send a Doll to defgivedolls (self): + Pass - the #Send Flowers * defgiveflowers (self): $ PassPanax Notoginseng - #Chocolate Delivery the defgivechocolate (self): + Pass A the + #The pursued person class - classschoolgirl (object): $ def __init__(self, name): $Self.name =name - - the #Suitor Class - classPursuit (givegift):Wuyi def __init__(self, Girl): theSelf. Girl =Girl - Wu defgivedolls (self): - PrintSelf. Girl.name,'give you a doll .' About $ defgiveflowers (self): - PrintSelf. Girl.name,'Send you flowers' - - defgivechocolate (self): A PrintSelf. Girl.name,'give you the chocolates .' + the - #proxy class $ classProxy (givegift): the def __init__(self, Girl): theSelf.proxy =Pursuit (Girl) the the #Send a Doll - defgivedolls (self): in self.proxy.GiveDolls () the the #Send Flowers About defgiveflowers (self): the self.proxy.GiveFlowers () the the #Chocolate Delivery + defgivechocolate (self): - self.proxy.GiveChocolate () the Bayi the if __name__=='__main__': theJiaojiao = Schoolgirl ('Jiaojiao') -Daili =Proxy (Jiaojiao) - Daili. Givedolls () the Daili. Giveflowers () theDaili. Givechocolate ()
The above class is designed like this:
Proxy proxies refer to the entity pursite of the proxy, providing the same interface as the Pursite to replace the entity
Andy
Source: http://www.cnblogs.com/onepiece-andy/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the author's consent must be in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Big talk involves pattern Python implementation-proxy mode