# 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 .
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