程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python singleton mode

編輯:Python

Preface

# Singleton mode means that a class can only create a unique instance , The following two methods, decorator and metaclass, are used to implement the singleton pattern , Because these two methods are the most versatile .

The singleton pattern

  • Preface
  • One . Using decorator to implement singleton mode
  • Two . Using metaclasses to implement the singleton pattern
  • summary

One . Using decorator to implement singleton mode

Create a dictionary , Set the class name to key , Class to the value of the key . Every time an instance is created , Will first see if there are instances , If there is an instance, you can directly return it .

def singleton(cls):
# Generate an empty dictionary 
instance = {
}
# judge Function to determine whether an instance exists , Yes : Return the instance . nothing : Create an instance and add it to the dictionary 
# cls Is instance name ,cls() It's creating instances 
def judge():
if cls not in instance:
instance[cls] = cls()
print(cls) # Output instance name 
return instance[cls] # Return the instance 
return judge # The decorator finally returns this function 
@singleton # The core of the decorator is to pass a function as an argument to another function singleton(ceshi)
class ceshi():
def __init__(self):
pass
p1 = ceshi()
p2 = ceshi()
p3 = ceshi()
print(id(p1),id(p2),id(p3)) # View all instances of id
#>>> <class '__main__.ceshi'>
#>>> 1298494399496 1298494399496 1298494399496

Of course you can also receive without a dictionary , You can use a value to accept an instance of a class

def singleton(cls):
# Instance of class 
instance = cls()
# judge Function to determine whether an instance exists , Yes : Return the instance . nothing : Create an instance and add it to the dictionary 
# cls Is instance name ,cls() It's creating instances 
def judge():
return instance # Return the instance 
return judge # The decorator finally returns this function 
@singleton # The core of the decorator is to pass a function as an argument to another function singleton(ceshi)
class ceshi():
def __init__(self):
pass
p1 = ceshi()
p2 = ceshi()
p3 = ceshi()
print(id(p1),id(p2),id(p3)) # View all instances of id
#>>> 2049299719176 2049299719176 2049299719176

If you generate a class that connects to the database , You can pass parameters to the class

def singleton(cls):
# Generate a default instance 
instance = cls('127.0.0.1','3306')
# cls Is instance name ,cls() It's creating instances 
def judge(*args,**kwargs):
if args or kwargs:
return cls(*args,**kwargs) # Pass in a new parameter to generate a new instance 
return instance # Return the default instance without new parameters 
return judge # The decorator finally returns this function 
@singleton # The core of the decorator is to pass a function as an argument to another function singleton(Mysql)
class Mysql():
def __init__(self,host,port):
self.host = host
self.port = port
# By default, a memory address is generated 
p1 = Mysql()
p2 = Mysql()
p3 = Mysql()
print(id(p1),id(p2),id(p3)) # View all instances of id 2436617218376 2436617218376 2436617218376
# The memory addresses for adding parameters are different 
a1 = Mysql('1.1.1.1', '1111')
a2 = Mysql('1.1.1.1', '2222')
print(id(a1),id(a2)) #2436617218504 2436617218440

Two . Using metaclasses to implement the singleton pattern

summary


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved