单例模式是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某一个类只能出现一个实例时,单例模式就能派上用场了。

     如某个服务器程序的配置信息存放在一个文件中,客户端通过一个 AppConfig 的类来读取配置文件的信息。如果在程序运行期间,有很多地方都需要使用配置文件的内容,也就是说,很多地方都需要创建 AppConfig 对象的实例,这就导致系统中存在多个 AppConfig 的实例对象,而这样会严重浪费内存资源,尤其是在配置文件内容很多的情况下。事实上,类似 AppConfig 这样的类,我们希望在程序运行期间只存在一个实例对象。

在python中,我们可以用多种方法来实现单例模式

1.文件导入

2.基于类

无法支持多线程

import threading

class Singleton(object): 
    def __init__(self):
        pass
    @classmethod 
    def instance(cls, *args, **kwargs): 
        if not hasattr(Singleton, "_instance"): 
            Singleton._instance = Singleton(*args, **kwargs) 
    return Singleton._instance
    
obj = Singleton.instance()

支持多线程

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

    def __init__(self):
        time.sleep(1)

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


# 第一次调用
def task(arg):
    obj = Singleton.instance()
    print(obj)
for i in range(10):
    t = threading.Thread(target=task,args=[i,])
    t.start()


# 第二次调用    
time.sleep(20)
obj = Singleton.instance()

3.使用__new__

无法支持多线程

class Singleton(object):
    
    Singleton._instance = None

    def __init__(self):
        pass


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

支持多线程

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, *args, **kwargs)
        return Singleton._instance
View Code

4.基于metaclass

-------------------------------示例(一)-------------------------------

# 创建对象

class SingletonType(type):

    def __call__(cls, *args, **kwargs):
        obj = super(SingletonType,cls).__call__(*args, **kwargs)   #type类帮创建__new__和__init__并返回
        return obj

class Foo(metaclass=SingletonType):
    def __init__(self,name):
        self.name = name

obj = Foo("alex")

-------------------------------示例(二)-------------------------------

import threading

class SingletonType(type):
    
    _instance_lock = threading.Lock()
    
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instance_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)
        return cls._instance

class Foo(metaclass=SingletonType):
    def __init__(self,name):
        self.name = name


obj1 = Foo('name')
obj2 = Foo('name')
print(obj1,obj2)
posted on 2018-01-30 17:00  Sober--  阅读(212)  评论(0编辑  收藏  举报