美文网首页
单例模式

单例模式

作者: butters001 | 来源:发表于2019-11-18 18:19 被阅读0次
    class Single(object):
        def __new__(cls, *args, **kwargs):
            if not hasattr(cls, '_instance'):
                cls._instance = super(Single, cls).__new__(cls, *args, **kwargs)
            return cls._instance
    
    
    class A(Single):
        pass
    
    
    def single2(cls):
        _instance = {}
    
        def wapper(*args, **kwargs):
            if cls not in _instance:
                _instance[cls] = cls(*args, **kwargs)
            return _instance[cls]
        return wapper
    
    
    @single2
    class B(object):
        pass
    

    相关文章

      网友评论

          本文标题:单例模式

          本文链接:https://www.haomeiwen.com/subject/tpybictx.html