我正在尝试在下面的代码中应用单例设计模式
class SMSMgr( object ):
_instance = None
def __init__(self):
self._allsp = []
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(SMSMgr, cls).__new__(
cls, *args, **kwargs)
return cls._instance
def loadsettings(self):
get_all_sp = ServiceProvider.objects.filter(status = False)
for obj in get_all_sp:
cla = obj.class_Name
a=globals()[str(obj.class_Name)](obj.userName,obj.password,obj.sendingurl)
self._allsp.append(a)
#print self._allsp
def send(self):
print "+++++++++++++++++++== Global send "
if __name__ == "__main__":
b = SMSMgr()
b.loadsettings()
print b._allsp
print "b end -------------------"
c = SMSMgr()
c.loadsettings()
print c._allsp
print "c end -------------------"
根据单例 b 对象应与 c 对象相同。但是当我运行上面的代码时,我得到了不同的对象b
,c
请建议我如何以单身人士的身份做到这一点。