Python:动态代理机制

ProxyFactory  传入hcls(被装饰类的处理器类型)作为init的参数, 传入 cls(被装饰类的类型)作为call的参数 

class ProxyFactory:
    def __init__(self, hcls):
        if issubclass(hcls, InvocationHandler) or hcls is InvocationHandler:
            self.hcls = hcls
        else:
            raise HandlerException(hcls)
 
    def __call__(self, cls):
        return Proxy(cls, self.hcls)
Proxy  传入cls(被装饰类的类型)、hcls(被装饰类的处理器类型)作为init的参数   

class Proxy:
    def __init__(self, cls, hcls):
        self.cls = cls
        self.hcls = hcls
        self.handlers = dict()
 
    def __call__(self, *args, **kwargs):
        self.obj = self.cls(*args, **kwargs)
        return self
 
    def __getattr__(self, attr):
        print('get attr', attr)
        isExist = hasattr(self.obj, attr)
        res = None
        if isExist:
            res = getattr(self.obj, attr)
            if isinstance(res, MethodType):
                if self.handlers.get(res) is None:
                    self.handlers[res] = self.hcls(self.obj, res)
                return self.handlers[res]
            else:
                return res
        return res
 

InvocationHandler 传入func(被装饰类的实例方法)作为init的参数

class InvocationHandler:
    def __init__(self, obj, func):
        self.obj = obj
        self.func = func
 
    def __call__(self, *args, **kwargs):
        print('handler:',self.func, args, kwargs)
        return self.func(*args, **kwargs)
 

HandlerException 处理器异常

class HandlerException(Exception):
    def __init__(self, cls):
        super(HandlerException, self).__init__(cls, 'is not a hanlder class')
 

Sample

@ProxyFactory(InvocationHandler)
class Sample:
    def __init__(self, age):
        self.age = age
    def foo(self):
        print('hello', self.age)
    def add(self, x, y):
        return x + y
 
 
s = Sample(12)
print(type(s))
s.foo()
s.add(1,2)
s.add(2,4)
print(s.age)
 

<class '__main__.Proxy'>
get attr foo
handler: <bound method Sample.foo of <__main__.Sample object at 0x000000B354B54BE0>> () {}
hello 12
get attr add
handler: <bound method Sample.add of <__main__.Sample object at 0x000000B354B54BE0>> (1, 2) {}
get attr add
handler: <bound method Sample.add of <__main__.Sample object at 0x000000B354B54BE0>> (2, 4) {}
get attr age
12
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值