python实现单例模式详解

一、单例模式

菜鸟教程-单例模式:https://www.runoob.com/design-pattern/singleton-pattern.html

二、python实现单例模式错误的示范

在网上看到的一个例子是使用双检锁实现单例模式,这个方法通过重载python对象的__new__ 方法,使得每个类只能被new一次。代码如下:

import threading


class Singleton(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        pass

    def __new__(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = object.__new__(cls)  
        return Singleton._instance


obj1 = Singleton()
obj2 = Singleton()
print(obj1,obj2)

上面的代码看似实现了单例模式,但是只是实现了一个单例模式的外壳,为什么这么说呢,我们在__init__函数里加一个打印语句看看。

import threading


class Singleton(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        ptint('__init__ is called.')

    def __new__(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = object.__new__(cls)  
        return Singleton._instance


obj1 = Singleton()
obj2 = Singleton()
print(obj1,obj2)

运行一下我们就会发现 __init__ 函数调用了两次,这是这段代码最大的问题,每次调用类时 __init__ 函数都会调用一次。虽然类只会被new一次,但是类的属性却会在类的使用过程中被不断覆盖,所以上面的代码只做到了类的单例,但是不能做到属性的单例。

有人说既然这样我把属性全部放在 __new__ 函数里初始化不就行了,这个做法在功能上没有问题,但是却违反了单一职责原则,__new__ 函数并不是负责初始化属性的功能,__init__ 函数才是。

另外上面的代码中将 Singleton 硬编码到了代码中,使得这个类不能被继承,因为当子类调用父类的 __new__ 函数时返回的不是子类的类型。所以我们需要将 Singleton 改成 cls__new__ 函数接受的类的type对象。

三、正确的示范

上面我们提到了 __init__ 函数调用多次的问题,也说明了直接在 __new__ 函数里初始化属性的问题,现在我们就来讨论一下如何正确的用 python实现单例模式。

我们现在面临的问题就是如何让 __init__ 函数只调用一次,最简单的思路就是让 __init__ 函数和 __new__ 函数一样,也使用一个标志和双检锁来确保线程安全和只调用一次,修改后的代码如下:

import threading


class Singleton(object):
    _lock = threading.Lock()

    def __init__(self):
        if not hasattr(self, '_init_flag'):
            with self._lock:
                if not hasattr(self, '_init_flag'):
                    self._init_falg = True
                    # 初始化属性
        			ptint('__init__ is called.')

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with cls._instance_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = object.__new__(cls)  
        return cls._instance


obj1 = Singleton()
obj2 = Singleton()
print(obj1,obj2)

现在我们的单例模式总算有点样子了,Singleton 类的 __new____init__ 函数都只会调用一次,并且这些都是线程安全的。

但是这样还不够,按照现在的方法,我们每次要定义一个单例模式的类时都需要手动去修改 __init__ 函数和 __new__ 函数,这有点麻烦。如果我们用的是 Java的话那就没办法了,这些麻烦事必要的,但我们使用的语言是python!

四、使用装饰器实现单例模式

从上一步单例模式的实现来看,我们每次要做到就是修改 __init__ 函数和 __new__ 函数,这简直就是为装饰器量身定做的应用场景。我们可以使用装饰器来替换类的 __init__ 函数和 __new__ 函数,将类原来的函数放在双检锁内部执行。代码如下:

from functools import wraps
import threading


def singleton():
    """
    单例模式装饰器
    :return:
    """
  	# 闭包绑定线程锁
    lock = threading.Lock()
    def decorator(cls):
        # 替换 __new__ 函数
        instance_attr = '_instance'
        # 获取原来的__new__函数 防止无限递归
		__origin_new__ = cls.__new__
        @wraps(__origin_new__)
        def __new__(cls_1, *args, **kwargs):
            if not hasattr(cls_1, instance_attr):
                with lock:
                    if not hasattr(cls_1, instance_attr):
                        setattr(cls_1, instance_attr, __origin_new__(cls_1, *args, **kwargs))
            return getattr(cls_1, instance_attr)
        cls.__new__ = __new__
		
        # 替换 __init__函数 原理同上
        init_flag = '_init_flag'
        __origin_init__ = cls.__init__
        @wraps(__origin_init__)
        def __init__(self, *args, **kwargs):
            if not hasattr(self, init_flag):
                with lock:
                    if not hasattr(self, init_flag):
                        __origin_init__(self, *args, **kwargs)
                        setattr(self, init_flag, True)
        cls.__init__ = __init__
        return cls
    return decorator

使用方法非常简单:

@singleton()
class Test:
	def __init__(self):
        # do something
        pass

需要注意的是装饰器要加括号,这是为了给每个类绑定一个线程锁,具体原理与单例模式无关,这里就不赘述了。另外使用了装饰器的类不需要修改 __new__ 函数,和普通的类一样使用就行。关于这个装饰器的具体实现原理我会找时间再写一篇博客。

参考

菜鸟教程-单例模式:https://www.runoob.com/design-pattern/singleton-pattern.html

博客园-听风。-Python中的单例模式的几种实现方式的及优化:https://www.cnblogs.com/huchong/p/8244279.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章