Python之单例格局总计,单例格局

By admin in 4858.com on 2019年4月3日

1、单例格局

单例情势,模式

1、单例情势

   
 单例格局分为三种:1.文书,二.类,叁.依照__new__方法实现单例情势,四.基于metaclass情势落成

2.类落成如下:

4858.com 1

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

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

类完成的-单例情势无法支撑拾二线程的状态
4858.com 2

import time
import threading
class Sigletion(object):
    _instance_lock=threading.Lock()
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs):
        if not hasattr(Sigletion,'_instance'):
            with Sigletion._instance_lock:
                if not hasattr(Sigletion,"_instace"):
                    Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

def task(arg):
    obj=Sigletion.instance()
    print(obj)

for i in range(10):
    t=threading.Thread(target=task,args=[i,])
    t.start()
time.sleep(20)
obj=Sigletion.instance()
print(obj)

类完毕的-单例形式不只怕支撑三十二线程的场合

 

3.基于__new__艺术完结单例格局

4858.com 3

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


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

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

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

基于__new__方法达成的单例形式

 

四.基于metaclass形式完结单例格局

Python之单例格局总计,单例格局。 

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

 

 

 

 

4858.com 4

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_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)

依据metaclass格局达成单例形式

 

一、单例格局单例方式分为多种:一.文书,二.类,三.基于__new__措施完结单例格局,四.基于metaclass情势实现二.类完毕如下:…

壹、单例情势

Python之单例形式总计,Python之情势总括

一、单例方式

   
a、单例方式分为多样:文件,类,基于__new__措施完毕单例形式,基于metaclass方式达成

    b、类达成如下:

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

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

    c、基于__new__办法达成单例形式

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


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

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

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

    d、据说metaclass格局落成单例情势

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_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)

 

4858.com,
1、单例格局a、单例形式分为三种:文件,类,基于__new__主意实现单例情势,基于metaclass情势达成…

   
a、单例形式分为种种:文件,类,基于__new__主意完成单例格局,基于metaclass格局完成

   
 单例情势分为八种:一.文本,二.类,3.依照__new__办法完毕单例方式,4.基于metaclass形式实现

    b、类完毕如下:

2.类完成如下:

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

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

4858.com 54858.com 6

    c、基于__new__办法达成单例格局

class Sigletion(objects):
    import time
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs)
        if not hasattr(Sigletion,'_instance'):
            Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

import threading

daf task(arg):
    obj=Sigletion.instance()
    print(obj)

for i in range(10):
    t=threading.Thread(target=task,args=[i,])
    t.start()
import time
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,*args,**kwargs)
       return Singleton._instance


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

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

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

类达成的-单例格局无法支撑拾2线程的情景

    d、基于metaclass格局达成单例方式

4858.com 74858.com 8

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_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)
import time
import threading
class Sigletion(object):
    _instance_lock=threading.Lock()
    def __init__(self):
        time.sleep(1)
    @classmethod
    def instance(cls,*args,**kwargs):
        if not hasattr(Sigletion,'_instance'):
            with Sigletion._instance_lock:
                if not hasattr(Sigletion,"_instace"):
                    Sigletion._instance=Sigletion(*args,**kwargs)
        return Sigletion._instance

def task(arg):
    obj=Sigletion.instance()
    print(obj)

for i in range(10):
    t=threading.Thread(target=task,args=[i,])
    t.start()
time.sleep(20)
obj=Sigletion.instance()
print(obj)

 

类达成的-单例情势不可能支撑四线程的图景

 

3.基于__new__方式实现单例方式

4858.com 94858.com 10

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


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

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

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

基于__new__方法实现的单例形式

 

四.基于metaclass格局完结单例情势

 

"""
1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)

# 第0步: 执行type的 __init__ 方法【类是type的对象】
class Foo:
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        pass

# 第1步: 执行type的 __call__ 方法
#        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
#        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
obj = Foo()
# 第2步:执行Foodef __call__ 方法
obj()
"""

 

 

 

 

4858.com 114858.com 12

import threading

class SingletonType(type):
    _instace_lock=threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instace_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)

依据metaclass格局完成单例情势

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有