欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中單例模式總結

 更新時間:2018年02月20日 10:07:55   作者:孟慶健  
單例模式(Singleton Pattern)是一種常用的軟件設計模式,該模式的主要目的是確保某一個類只有一個實例存在。當你希望在整個系統(tǒng)中,某個類只能出現(xiàn)一個實例時,單例對象就能派上用場。

一、單例模式

    a、單例模式分為四種:文件,類,基于__new__方法實現(xiàn)單例模式,基于metaclass方式實現(xiàn)

    b、類實現(xiàn)如下:

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__方法實現(xiàn)單例模式

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方式實現(xiàn)單例模式

"""
1.對象是類創(chuàng)建,創(chuàng)建對象時候類的__init__方法自動執(zhí)行,對象()執(zhí)行類的 __call__ 方法
2.類是type創(chuàng)建,創(chuàng)建類時候type的__init__方法自動執(zhí)行,類() 執(zhí)行type的 __call__方法(類的__new__方法,類的__init__方法)

# 第0步: 執(zhí)行type的 __init__ 方法【類是type的對象】
class Foo:
  def __init__(self):
    pass

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

# 第1步: 執(zhí)行type的 __call__ 方法
#    1.1 調用 Foo類(是type的對象)的 __new__方法,用于創(chuàng)建對象。
#    1.2 調用 Foo類(是type的對象)的 __init__方法,用于對對象初始化。
obj = Foo()
# 第2步:執(zhí)行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)

相關文章

  • Python multiprocessing.Manager介紹和實例(進程間共享數(shù)據(jù))

    Python multiprocessing.Manager介紹和實例(進程間共享數(shù)據(jù))

    這篇文章主要介紹了Python multiprocessing.Manager介紹和實例(進程間共享數(shù)據(jù)),本文介紹了Manager的dict、list使用例子,同時介紹了namespace對象,需要的朋友可以參考下
    2014-11-11
  • python-str,list,set間的轉換實例

    python-str,list,set間的轉換實例

    今天小編就為大家分享一篇python-str,list,set間的轉換實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python如何實現(xiàn)轉換URL詳解

    Python如何實現(xiàn)轉換URL詳解

    這篇文章主要介紹了Python如何實現(xiàn)轉換URL詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • python?sklearn數(shù)據(jù)預處理之數(shù)據(jù)縮放詳解

    python?sklearn數(shù)據(jù)預處理之數(shù)據(jù)縮放詳解

    數(shù)據(jù)的預處理是數(shù)據(jù)分析,或者機器學習訓練前的重要步驟,這篇文章主要為大家詳細介紹了sklearn數(shù)據(jù)預處理中數(shù)據(jù)縮放的相關知識,感興趣的小伙伴可以學習一下
    2023-10-10
  • python學習之新式類和舊式類講解

    python學習之新式類和舊式類講解

    這篇文章主要介紹了python 新式類和舊式類,文中比較詳細的介紹了相關內容,有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • python生成器在讀取接口用例中應用解析

    python生成器在讀取接口用例中應用解析

    這篇文章主要為大家介紹了python生成器在讀取接口用例中應用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • python的sorted用法詳解

    python的sorted用法詳解

    在本篇文章里小編給大家整理了關于python的sorted用法以及相關實例內容,有需要的朋友們參考學習下。
    2019-06-06
  • Python 窗體(tkinter)按鈕 位置實例

    Python 窗體(tkinter)按鈕 位置實例

    今天小編就為大家分享一篇Python 窗體(tkinter)按鈕 位置實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python語音識別實踐之百度語音API

    python語音識別實踐之百度語音API

    這篇文章主要為大家詳細介紹了python語音識別實踐之百度語音API,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • python讀取文件由于編碼問題失敗匯總以及解決辦法

    python讀取文件由于編碼問題失敗匯總以及解決辦法

    這篇文章主要給大家介紹了關于python讀取文件由于編碼問題失敗匯總以及解決辦法的相關資料,文件編碼錯誤指的是在Python讀取文件的過程中出現(xiàn)的編碼不匹配的問題,需要的朋友可以參考下
    2023-10-10

最新評論