Python设计模式之代理模式实例

人来到这世界后,命运注定了他必须要拼搏,奋斗,坚持,勇敢地走下去,走出属于自己的道路,没有人能不劳而获。

翻墙常用的方式就是使用代理(Proxy),其基本过程如下:

浏览器<-->代理服务器<-->服务器

如果浏览器请求不到服务器,或者服务器无法响应浏览器,我们可以设定将浏览器的请求传递给代理服务器,代理服务器将请求转发给服务器。然后,代理服务器将服务器的响应内容传递给浏览器。当然,代理服务器在得到请求或者响应内容的时候,本身也可以做些处理,例如缓存静态内容以加速,或者说提取请求内容或者响应内容做些正当或者不正当的分析。这种翻墙方式,就是设计模式中代理模式(Proxy Pattern)的一个具体例子。

维基百科对代理模式做了以下解释:


In computer programming, the proxy pattern is a software design pattern. A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

基于面向过程实现的代理模式

下面是一段体现该设计模式中心的面向过程的python代码:


def hello():
print 'hi, i am hello'

def proxy():
print 'prepare....'
hello()
print 'finish....'

if __name__ == '__main__':
proxy()


运行结果:

prepare....
hi, i am hello
finish....

有没有想到装饰器?


基于面向对象实现的代理模式


class AbstractSubject(object):

def __init__(self):
pass

def request(self):
pass

class RealSubject(AbstractSubject):

def __init__(self):
pass
def request(self):
print 'hi, i am RealSubject'

class ProxySubject(AbstractSubject):

def __init__(self):
self.__rs = RealSubject()

def request(self):
self.__beforeRequest()
self.__rs.request()
self.__afterRequest()

def __beforeRequest(self):
print 'prepare....'

def __afterRequest(self):
print 'finish....'

if __name__ == '__main__':
subject = ProxySubject()
subject.request()

如果RealSubject的初始化函数init有参数,代理类ProxySubject可以作两种方式的修改: 第一种: ProxySubject的init方法同样也有参数,初始化代理类的时候将初始化参数传递给RealSubject。 第二种: 将ProxySubject的init方法改为:


def __init__(self):
self.__rs = None

将ProxySubject的request方法改为:

def request(self, *args, **kwargs):
if self.__rs == None:
self.__rs = RealSubject(*args, **kwargs)
self.__beforeRequest()
self.__rs.request()
self.__afterRequest()

的类似形式。

以上就是Python设计模式之代理模式实例。牡丹花好,终须绿叶扶持。更多关于Python设计模式之代理模式实例请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战