十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
单例的实现方式:
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__需要传入参数
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
成都创新互联公司是一家集网站建设,化州企业网站建设,化州品牌网站建设,网站定制,化州网站建设报价,网络营销,网络优化,化州网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。