newbie_evolve's Blog

Happy coding
Python设计模式之“抽象工厂模式”
python常用的内建函数

Python设计模式之“代理模式”

newbie_evolve posted @ 2013年8月19日 10:44 in python之年 , 1380 阅读

代理模式:为其他对象提供代理以控制对这个对象的访问。代理模式在客户端和目标对象之间起到中介的作用,这样起到了的作用和保护目标对象的作用。

 

class Interface():#接口类,RealSuject与Proxy都实现接口类中的Request方法

    def Request(self):

        pass


class RealSubject(Interface):

    def Request(self):

        print "I want to buy a flower"  


class Proxy(Interface):#Proxy与RealSubject类相关联,调用其Request()方法,这样客户端只知道Proxy不清楚RealSubject类的对象

    def Request(self):

        self.realsubject = RealSubject()

        self.realsubject.Request()

        

if __name__ == "__main__":

    p = Proxy()

    p.Request()

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter