前言
单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场
比如,某个服务器程序的配置信息存放在一个文件中,客户端通过一个 AppConfig 的类来读取配置文件的信息。如果在程序运行期间,有很多地方都需要使用配置文件的内容,也就是说,很多地方都需要创建 AppConfig 对象的实例,这就导致系统中存在多个 AppConfig 的实例对象,而这样会严重浪费内存资源,尤其是在配置文件内容很多的情况下。事实上,类似 AppConfig 这样的类,我们希望在程序运行期间只存在一个实例对象。
目录
单例模式
多次实例化的结果指向同一个实例
单例模式的实现方式一:(绑定类的方法)
import settings
class Mysql:
__instance = None
def __init__(self, IP, PORT):
self.IP = IP
self.PORT = PORT
@classmethod
def from_conf(cls):
if cls.__instance is None:
cls.__instance=cls(settings.IP,settings.PORT)
return cls.__instance
obj1 = Mysql.from_conf()
obj2 = Mysql.from_conf()
obj3 = Mysql("1.1.1.3","2206")
print(obj1) #<__main__.Mysql object at 0x0000000002708278>
print(obj2) #<__main__.Mysql object at 0x0000000002708278>
print(obj3) #<__main__.Mysql object at 0x0000000002708550>
#settings.py
IP="1.1.1.2"
PORT= "3306"
单例模式的实现方式二:(@singleton装饰器)
def singleton(cls):
_instance=cls(settings.IP,settings.PORT)
def wrapper(*args,**kwargs):
if len(args)!=0 or len(kwargs)!=0:
obj=cls(*args,**kwargs)
return obj
return _instance
return wrapper
@singleton #Mysql=singleton(Mysql) Mysql=wrapper
class Mysql:
def __init__(self,IP,PORT):
self.IP=IP
self.PORT=PORT
obj1=Mysql()
obj2=Mysql()
obj3=Mysql()
obj4=Mysql("1.1.1.2",2206)
print(obj1) #<__main__.Mysql object at 0x0000000002578278>
print(obj2) #<__main__.Mysql object at 0x0000000002578278>
print(obj3) #<__main__.Mysql object at 0x0000000002578278>
print(obj4) #<__main__.Mysql object at 0x0000000002578588>
单例模式的实现方式三:(元类)
import settings
class Mymeta(type):
def __init__(self,class_name,class_bases,class_dic):
#self 是Mysql这个类
self._instance=self(settings.IP,settings.PORT)
def __call__(self, *args, **kwargs):
if len(args)!=0 or len(kwargs)!=0:
# self 是Mysql这个类
obj=self.__new__(self)
self.__init__(obj,*args,**kwargs)
return obj
else:
return self._instance
class Mysql(object,metaclass=Mymeta): #Mysql=Mymeta(....)
def __init__(self,IP,PORT):
self.IP=IP
self.PORT=PORT
obj1=Mysql()
obj2=Mysql()
obj3=Mysql("1.1.1.3",6609)
print(obj1) #<__main__.Mysql object at 0x00000000021D8550>
print(obj2) #<__main__.Mysql object at 0x00000000021D8550>
print(obj3) #<__main__.Mysql object at 0x00000000021D8550>
单例模式的实现方式四:(导模块 –第一次导模块会创造名称空间,第二次导直接用第一次造好的名称空间)
#singleton.py
import settings
class Mysql:
def __init__(self, IP, PORT):
self.IP = IP
self.PORT = PORT
instance=Mysql(settings.IP,settings.PORT)
#settings.py
IP="1.1.1.2"
PORT= "3306"
def f1():
from singleton import instance
print(instance) #<singleton.Mysql object at 0x00000000021B8630>
def f2():
from singleton import instance,Mysql
print(instance) #<singleton.Mysql object at 0x00000000021B8630>
obj=Mysql("1.1.1.6",3306)
print(obj) #<singleton.Mysql object at 0x0000000002104978>
f1()
f2()