单例的实现方式-成都快上网建站

单例的实现方式

单例的实现方式:
1、基于类

#encoding=utf-8
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

s1 = Singleton.instance()
s2 = Singleton.instance()

print(s1 is s2)

支持多线程:
#encoding=utf-8
import threading

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

    def __init__(self):
        pass

    @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()

obj = Singleton.instance()
print(obj)

2、利用__new__()
#encoding=utf-8

class Singleton(object):

    def __init__(self):
        pass

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

p1 = Singleton()
p2 = Singleton()
print(p1 is p2)

支持多线程方式

#encoding=utf-8
import threading

class Singleton(object):

    _instance_lock = threading.Lock()

    def __init__(self):
        pass

    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

def task():
    obj = Singleton()
    print(obj)

for i in range(20):
    t = threading.Thread(target=task,args=())
    t.start()

3、利用装饰器
#encoding=utf-8
import threading

def Singleton(cls):

    _instance = {}

    def _singleton(*args,**kwargs):
        if cls not in _instance:
            _instance[cls] = cls(*args,**kwargs)
        return _instance[cls]

    return _singleton

@Singleton
class Person(object):#Person = Singleton(Person)
    a =  10

    def __init__(self,x=0):
        self.x = x

p = Person(2)#_singleton(2)
p2 = Person(3)

print(p is p2)

print(p.x)
print(p2.x)

示例1:
#encoding=utf-8
class Person(object):
    __instance = None#定义一个类变量,用于绑定实例对象
    def __init__(self,name,age):
        self.name = name
        self.age = age

    @classmethod
    def get_instance(cls,name,age):
        if cls.__instance == None:
            cls.__instance =Person(name,age)
        return cls.__instance

    def get_info(self):
        return self.name + ":" + str(self.age)

p1 = Person.get_instance("xxx",20)

p2 = Person.get_instance("yyy",50)

print(p1 is p2)

print(p1.get_info())
print(p2.get_info())

#此种实现方式无法在生成实例后改变实例变量的值

示例2:
#encoding=utf-8
class Singleton(object):
    _instance = None

    def __new__(cls,a,b,*args,**kwargs):
        if cls._instance == None:
            cls._instance = super(Singleton,cls).__new__(cls,*args,**kwargs)
        return cls._instance

class Person(Singleton):
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def getInfo(self):
        return "姓名: %s,年龄: %s" %(self.name,self.age)

p1 = Person("张三",20)
print(p1.getInfo())

p2 = Person("李四",20)
print(p1.getInfo())

print(p1 is p2)

print("类变量: ",Person._instance)

示例3:
#encoding=utf-8

class Person(object):

    __instance = None#创建一个类变量用户存储类的实例对象

    def __init__(self,name,age):

        self.name = name
        self.age = age

    def __new__(cls,name,age):#自定义Person自己的__new__()方法用于产生实例对象,此处需要传入name,age两个参数
        if cls.__instance == None:#保证实例对象__instance只会被赋值一次,如果之前已经创建过实例,直接返回之前创建的实例
            #cls.__instance = super(Person,cls).__new__(cls)
            cls.__instance = object.__new__(cls)#调用父类的__new__方法创建对象

        return cls.__instance#返回实例对象

    def getInfo(self):

        return "姓名: %s,年龄: %s" %(self.name,self.age)

p1 = Person("h",20)
print(p1.getInfo())

p2 = Person("kkk",30)
print(p1.getInfo())
print(p2.getInfo())

print(p1 is p2)

'''
以上代码执行过程:
p1 = Person("h",20)
首先调用__new__方法产生实例p1,此时Person的__instance为none所以会创建一个对象,
然后调用__init__方法,把返回的实例对象__instance传入self中,并初始实例变量name,age

print(p2.getInfo())
首先调用__new__方法产生实例p2,此时Person的__instance为不为None,为p1,所以会直接返回对象p1,
然后调用__init__方法,把返回的实例对象__instance传入self中,并用新的name,age初始实例变量name,age 
'''    

如果__init__有其他参数,__new__需要传入参数

网页名称:单例的实现方式
本文网址:http://kswjz.com/article/gddsgj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流