Big talk involves pattern Python implementation-proxy mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.