一.什么叫单例模式
基于某种方法通过类实例化的得到的对象,或者说内存地址指向同一个.
总共有4种方式.最后一个方式就是当作包导入使用。
方式一
方式一
settings.py文件中内容:
ip = '1.1.1.1'
port = 3306
版本一
import settings
class Mysql:
__instance = None
def __init__(self, ip, port):
self.ip = ip
self.port = port
@classmethod
def from_conf(cls):
if not cls.__instance: #cls = Mysql ==>如果Mysql.__instance为假,执行下面代码
cls.__instance = cls(settings.ip, settings.port)# ===>调用Mysql(settings.ip, settings.port)进行实例化
return cls.__instance
obj1 = Mysql.from_conf()
obj2 = Mysql.from_conf()
obj3 = Mysql('1.1.1.10', 3302)
print(obj1)
print(obj2)
print(obj3)
版本二
class Mysql:
def __init__(self, ip, port):
self.ip = ip
self.port = port
@classmethod
def from_conf(cls):
if not hasattr(cls,'_%s__instance'%cls.__name__):#通过反射方法hasattr来判断
cls.__instance = cls(settings.ip, settings.port)# ===>调用Mysql(settings.ip, settings.port)进行实例化
return cls.__instance
obj1 = Mysql.from_conf()
obj2 = Mysql.from_conf()
obj3 = Mysql('1.1.1.10', 3302)
print(obj1)
print(obj2)
print(obj3)
方式二
def singleton(cls):
_instance = cls(settings.ip, settings.port) # 先导入配置文件settings中的内容
def wrapper(*args, **kwargs): # *args,**kwargs用来接受参数
if args or kwargs: # 如果有参数
obj = cls(*args, **kwargs) # 实例化对象obj
return obj
return _instance # 这说明wrapper没有参数,就返回默认的_instance
return wrapper
@singleton # Mysql=singleton(Mysql)==>Mysql = wrapper
class Mysql:
def __init__(self, ip, port):
self.ip = ip
self.port = port
obj1 = Mysql() # 这里的Mysql其实是wrapper函数,加括号调用wrapper
obj2 = Mysql()
obj3 = Mysql('1.1.1.10', 3302) # obj3 = wrapper('1.1.1.10',3302)
print(obj1)
print(obj2)
print(obj3)
方式三
class Mymeta(type):
def __init__(self,class_name,class_bases,class_dic):#self = Mysql
super().__init__(class_name,class_bases,class_dic)
self.__instance = self(settings.ip,settings.port)
def __call__(self, *args, **kwargs):
if args or kwargs:
obj = self.__new__(self) #创建一个空对象
self.__init__(obj,*args, **kwargs) #初始化空对象
return obj #返回初始化好的对象
return self.__instance#返回一个创建好的Msql对象
class Mysql(metaclass=Mymeta): #Mysql = Mymeta(...)
def __init__(self, ip, port):
self.ip = ip
self.port = port
obj1 = Mysql()
obj2 = Mysql()
obj3 = Mysql('1.1.1.2', 3307)
print(obj1)
print(obj2)
print(obj3)