• 用__new__ 创建单例模式


    用 __new__方法

    #_*_ encoding: utf-8 _*_   @author: ty  hery   2019/9/6
    
    class Singleton(object):
        def __new__(cls):
            if not hasattr(cls,'_instance'):
                cls._instance=super(Singleton,cls).__new__(cls)
    #            cls.__instance = object.__new__(cls) #这样也可以
            return cls._instance
    a = Singleton()
    b = Singleton()
    c = Singleton()
    print(a,id(a))
    print(b,id(b))
    print(c,id(c))
    输出:
    <__main__.Singleton object at 0x000000000287A240> 42443328
    <__main__.Singleton object at 0x000000000287A240> 42443328
    <__main__.Singleton object at 0x000000000287A240> 42443328
    

    共享属性方法

    # 共享属性:创建实例时把所有实例的__dict__指向同一个字典,这样它们具有相同的属性和方法.
    class Borg(object):
        _state = {}
        def __new__(cls, *args, **kw):
            ob = super(Borg, cls).__new__(cls, *args, **kw)
            ob.__dict__ = cls._state
            return ob
    class MyClass2(Borg):
        a = 1
    b1 = MyClass2()
    b2 = MyClass2()
    b3 = MyClass2()
    b1.a='萨菲的'
    b2.a='萨菲02'
    b1.a='萨菲03'
    MyClass2.a = 333
    print(id(b1),id(b2),id(b3))
    print(b1.a,b3.a,b2.a)
    print(id(b1.a),id(b2.a),id(b3.a))
    输出: 
    42902024 42902080 42901688
    萨菲03 萨菲03 萨菲03
    167568080 167568080 167568080
    

    装饰器版本

    # 装饰器版本
    def singleton(cls, *args, **kw):
        instances = {}
        def getinstance():
            if cls not in instances:
                instances[cls] = cls(*args, **kw)
            return instances[cls]
        return getinstance
    @singleton
    class MyClass:
        a = 1
    b1 = MyClass()
    b2 = MyClass()
    b3 = MyClass()
    print(id(b1),id(b2),id(b3))
    print(b1.a,b3.a,b2.a)
    输出:
    42712928 42712928 42712928
    1 1 1
    

    类方法版

    class Foo():
        __v = None
        @classmethod
        def get_instance(cls):
            if cls.__v:
                print("true: 这是cls._-v",cls.__v)
                return cls.__v
            else:
                print("false: 这是cls._-v", cls.__v)
                cls.__v = Foo()
                print("false_1: 这是cls._-v", cls.__v)
                return cls.__v
    obj = Foo.get_instance()
    obj1 = Foo.get_instance()
    obj2 = Foo.get_instance()
    obj3 = Foo.get_instance()
    print(obj,'哈哈',id(obj))
    print(obj1,'哈哈',id(obj1))
    print(obj2,'哈哈',id(obj2))
    print(obj3,'哈哈',id(obj3))
    

    输出:

    false: 这是cls._-v None
    false_1: 这是cls._-v <__main__.Foo object at 0x0000000009FC4F98>
    true: 这是cls._-v <__main__.Foo object at 0x0000000009FC4F98>
    true: 这是cls._-v <__main__.Foo object at 0x0000000009FC4F98>
    true: 这是cls._-v <__main__.Foo object at 0x0000000009FC4F98>
    <__main__.Foo object at 0x0000000009FC4F98> 哈哈 167530392
    <__main__.Foo object at 0x0000000009FC4F98> 哈哈 167530392
    <__main__.Foo object at 0x0000000009FC4F98> 哈哈 167530392
    <__main__.Foo object at 0x0000000009FC4F98> 哈哈 167530392
    

    单例默认:

    class Singleton(object):
        def __new__(cls,*args,**kwargs):
            if not hasattr(cls,'_instance'):
                print('你好啊',)
                cls._instance = super(Singleton,cls).__new__(cls)
                print(cls._instance, '阿斯蒂芬01')
                return cls._instance
            print( '阿斯蒂芬')
            print(cls._instance, '阿斯蒂芬02')
            return cls._instance
            
    a = Singleton()
    print('这是a',id(a),a)
    b = Singleton()
    print('这是b',id(b),b)
    

    输出:

    你好啊
    <__main__.Singleton object at 0x000000000288BEF0> 阿斯蒂芬01
    这是a 42516208 <__main__.Singleton object at 0x000000000288BEF0>
    阿斯蒂芬
    <__main__.Singleton object at 0x000000000288BEF0> 阿斯蒂芬02
    这是b 42516208 <__main__.Singleton object at 0x000000000288BEF0>
    
    写入自己的博客中才能记得长久
  • 相关阅读:
    elementUI table中的button 操作
    数组
    数组,字符串,数字之间的相互转换
    Element upload
    Vue中涉及到的三目运算与v-if结合
    ElementUI的input用法
    javascript的正则表达式
    Vue表单+Vue全局指令 v-focus 的引用
    ztree 异步加载大数据(一)
    vue mixins 混入项目实例
  • 原文地址:https://www.cnblogs.com/heris/p/14642511.html
Copyright © 2020-2023  润新知