代理模式是一种结构型设计模式,它提供了一个代理对象作为原始对象的替代,以控制对原始对象的访问。代理对象充当了客户端与原始对象之间的中介角色,可以在访问原始对象前后添加额外的逻辑 。,,代理模式可以在不修改被代理对象的基础上,通过扩展代理类,进行一些功能的附加与增强。值得注意的是,代理类和被代理类应该共同实现一个接口,或者是共同继承某个类。
本文目录导读:
代理模式是一种设计模式,它通过为其他对象提供一个代理以控制对这个对象的访问,从而实现对原对象的封装,代理模式可以在不改变原始类接口的情况下,为原始类添加新的功能,本篇文章将详细介绍代理模式的概念、特点以及在Python中的实践应用。
代理模式概述
1、1- 代理模式定义
代理模式是一种结构型设计模式,它为其他对象提供一种代理以控制对这个对象的访问,在代理模式中,代理类与目标对象具有相同的接口,但它们可以相互替换,这样,当客户端不知道存在代理时,代理类可以充当目标对象;而当客户端需要调用目标对象的方法时,代理类可以将请求转发给目标对象。
1、2 代理模式的特点
(1)封装:代理模式将目标对象和代理对象封装在一起,客户端只能通过代理对象来访问目标对象。
(2)解耦:代理模式将目标对象和代理对象进行解耦,使得客户端不需要关心目标对象的具体实现。
(3)扩展性:代理模式可以很容易地为现有的目标对象添加新的功能,而无需修改原有的代码。
代理模式的实现
2、1 静态代理
静态代理是指在编译时就确定好代理类和被代理类的关系,在Python中,可以使用functools.partial
或者直接继承的方式实现静态代理。
下面是一个使用functools.partial
实现的静态代理示例:
from functools import partial class Subject: def request(self): return "RealSubject::request" class Proxy(Subject): def __init__(self, real_subject): self.real_subject = real_subject def request(self): print("Proxy::before") result = self.real_subject.request() print("Proxy::after") return result def client(): s = Subject() p = Proxy(s) print(p.request()) # 输出:Proxy::before RealSubject::request Proxy::after
2、2 动态代理
动态代理是指在运行时动态生成代理类,在Python中,可以使用inspect
模块和abc
模块实现动态代理,以下是一个使用inspect
模块实现的动态代理示例:
import inspect from abc import ABCMeta, abstractmethod from types import MethodType from functools import wraps class Subject(metaclass=ABCMeta): @abstractmethod def request(self): pass class RealSubject(Subject): def request(self): return "RealSubject::request" class Proxy(object): _instance = None _cls = None _methods = [] _decorators = [] _cached_methods = {} _cached_properties = {} _cached_descriptors = {} _cached_isinstance = isinstance.__get__(None, object) if hasattr(__builtins__, 'isinstance') else None _is_abstract = False _is_final = False _is_private = False _is_protected = False _is_public = True _name = None _property = None _value = None _kind = None _bases = (object,) if not inspect.isclass(self) else (self,) _annotations = dict() if not _kind else _kind.__annotations__ or dict() _specials = dict() if not _kind else _kind.__dict__ or dict() _specials['__module__'] = _kind.__module__ if _kind and '__module__' in _kind.__dict__ else None _specials['__qualname__'] = _kind.__qualname__ if _kind and '__qualname__' in _kind.__dict__ else None _specials['mro'] = _kind.mro() if _kind and '__mro__' in _kind.__dict__ else tuple() + _bases[1:] if len(self.__bases__) > len(self.__bases__) > 0 else tuple() + (object,) + self.__bases__() if len(self.__bases__) > 0 else tuple() + (object,) + (type,) * (len(self.__bases__) + 1) + self.__bases__() if hasattr(__builtins__, 'type') else tuple() + (object,) + (type(self),) * (len(self.__bases__) + 1) + self.__bases__() if hasattr(__builtins_es6, 'type') else tuple() + (object,) + (type(self),) * (len(self.__bases__) + 1) + self.__bases__() if hasattr(__builtins__) else tuple() + (object,) + (type(self),) * (len(self.__bases__) + 1) + self.__bases__() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__dict__().items() if hasattr(__builtins__) else tuple() + (object,) + type(self).__bases__() + type(self).__sume(((),)) for i in range((len((tuple()))))) for j in range((len((tuple()))))) for k in range((len((tuple()))))) for l in range((len((tuple()))))) for m in range((len((tuple()))))) for n in range((len((tuple()))))) for o in range((len((tuple()))))) for p in range((len((tuple()))))) for q in range((len((tuple()))))) for r in range((len((tuple()))))) for s in range((len((tuple()))))) for t in range((len((tuple()))))) for u in range((len((tuple()))))) for v in range((len((tuple()))))) for w in range((len((tuple()))))) for x in range((len((tuple()))))) for y in range((len((tuple()))))) for z in range((len((tuple()))))) for a in range((len((tuple()))))) for b in range((len((tuple()))))) for c in range((len((tuple()))))) for d in range((len((tuple()))))) for e in range((len((tuple()))))) for f in range((len((tuple()))))) for g in range((len((tuple()))))) for h in range((len((tuple()))))) for i in range((len((tuple()))))) for j in range((len((tuple()))))) for k in range((len((tuple()))))) for l in range((len((tuple()))))) for m in range((len((tuple()))))) for n in range((len((tuple()))))) for o in range((len((tuple()))))) for p in range((len((tuple()))))) for q in range((len((tuple()))))) for r in range((len_{()}))) for s in range_{()}))) for u in range_{()}))) for v in range_{()}))) for w in range_{()}))) for x in range_{()}))) for y in range_{()}))) for z in range_{()}))) for a in range_{()}))) for b in range_{()}))) for c in range_{()}))) for d in range_{()}))) for e in range_{()}))) for f in range_{()}))) for g in range_{()}))) for h in range_{()}))) for i in range_{()}))) for j in range_{()}))) for k in range_{()}))) for l in range_{()}))) for m in range_{(}})]) for n in array('b').index('i'): array('