• 创建单例模式的六种方式


    一、基于装饰器实现单例模式

    #装饰器实现单例模式
    
    def singleTon(cls):
        def wrapper(*args,**kwargs):
            if not hasattr(cls,'_instance'):
                cls._instance = cls(*args,**kwargs)
            return cls._instance
        return wrapper
    
    @singleTon
    class SingleTonTest:
        pass
    
    s1 = SingleTonTest() 
    s2 = SingleTonTest() 
    print(s1) #<__main__.SingleTonTest object at 0x0000000000656860>
    print(s2) #<__main__.SingleTonTest object at 0x0000000000656860>

    二、基于元类实现单例模式

    class MyType(type):
    
        def __call__(self, *args, **kwargs):
            """
            self指的是SingleTonTest类
            :param args:
            :param kwargs:
            :return:
            """
            if not hasattr(self,'_instance'):
                self._instance = super(MyType,self).__call__(*args,**kwargs)
            return self._instance
    
    class SingleTonTest(metaclass=MyType):
        pass
    
    #SingleTonTest是元类MyType的对象,对象加括号执行元类中的__call__方法
    s1 = SingleTonTest()
    s2 = SingleTonTest()
    print(s1) #<__main__.SingleTonTest object at 0x0000000000646898>
    print(s2) #<__main__.SingleTonTest object at 0x0000000000646898>

    三、基于__new__方法实现单例模式

    class SingleTonTest:
    
        def __new__(cls, *args, **kwargs):
            """
            cls:<class '__main__.SingleTonTest'>
            :param args:
            :param kwargs:
            :return:
            """
            if not hasattr(cls,'_instance'):
                cls._instance = super(SingleTonTest,cls).__new__(cls, *args, **kwargs)
            return cls._instance
    
    s1 = SingleTonTest()
    s2 = SingleTonTest()
    print(s1) #<__main__.SingleTonTest object at 0x0000000000BD68D0>
    print(s2) #<__main__.SingleTonTest object at 0x0000000000BD68D0>

    四、基于静态方法实现单例模式

    class SingleTonTest:
    
        @classmethod
        def getInstance(cls):
            if not hasattr(cls,'_instance'):
                cls._instance = cls()
            return cls._instance
    
    s1 = SingleTonTest.getInstance()
    s2 = SingleTonTest.getInstance()
    print(s1) #<__main__.SingleTonTest object at 0x0000000000646828>
    print(s2) #<__main__.SingleTonTest object at 0x0000000000646828>

    五、基于模块实现单例模式

    class SingleTonTest:
        pass
    
    s = SingleTonTest()
    
    #在另一个py文件中导入该类对象
    from base import s
    
    print(s)

    六、Monostate单例模式

    将所有创建对象的__dict__属性指向同一个字典,这样所有的对象实现了数据的共享。

    class Borg:
    
        __shared_state={}
    
        def __new__(cls, *args, **kwargs):
            obj = super(Borg,cls).__new__(cls, *args, **kwargs)
            obj.__dict__ = cls.__shared_state
            return obj
    
    #共享同一个字典,一个对象改变属性,另一个也跟着改变
    b1 = Borg()
    b1.name = 'Bright'
    b2 = Borg()
    
    print(b1.__dict__) #{'name': 'Bright'}
    print(b2.__dict__) #{'name': 'Bright'}

      将__shared_state变量作为一个中间变量,并且赋值给python中存储类所有对象状态的__dict__变量,这在实例后,会产生两个不同的对象,但是b1.__dict__和b2.__dict__都指向了__shared_state的内存地址。

    注意,这种单例模式是数据的单例,对象还是两个不一样的对象。

  • 相关阅读:
    C#拾遗系列(2):属性
    都江堰我永远支持你
    【号外】2011 Autodesk开发者日又来啦~~
    DB_RUNRECOVERY: Fatal error, run database recovery StackTrace问题的解决办法
    [书友交流] <Autodesk 地理信息系统解决方案FDO、MapGuide、AutoCAD Map 3D二次开发指南>读书交流贴
    Autodesk Infrastructure Map Server 2012 SP1 Released!
    Map3D中获取地图中心及Zoom到新的中心点
    Introduction to MapGuide OpenSource – from FOSS4G in Denver
    获取选中地物的Geometry对象,及把Geometry对象显示在地图上
    [在线技术活动]Civil 3D 部件编辑器详解
  • 原文地址:https://www.cnblogs.com/shenjianping/p/11681078.html
Copyright © 2020-2023  润新知