代理模式,在调用真实方法之前后分别执行所需的操作。
class RealRequest(object):
def __init__(self):
super().__init__()
def request(self):
print("request")
class Proxy(object):
def __init__(self, real_request):
self.real_request = real_request
def request(self):
self.before_request()
self.real_request.request()
self.after_request()
def before_request(self):
print("before_request")
def after_request(self):
print("after_request")
def main():
real_request = RealRequest()
proxy = Proxy(real_request)
proxy.request()
if __name__ == '__main__':
main()
网友评论