zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python设计模式之单例模式实例

2023-06-13 09:15:25 时间

注:使用的是Python2.7。

一个简单实现

复制代码代码如下:

classFoo(object):
   __instance=None
   def__init__(self):
       pass
   @classmethod
   defgetinstance(cls):
       if(cls.__instance==None):
           cls.__instance=Foo()
       returncls.__instance

if__name__=="__main__":
   foo1=Foo.getinstance()
   foo2=Foo.getinstance()
   printid(foo1)
   printid(foo2)
   printid(Foo())


输出的前两个结果是相同的(id(foo1)与id(foo2)的值相同),第三个结果和前两个不同。这里类方法getinstance()用于获取单例,但是类本身也可以实例化,这样的方式其实并不符合单例模式的要求。但是这样做也有好处,代码简单,大家约定好这样子调用就行了。但是最好在类的命名上也体现了出来这是一个单例类,例如Foo_singleton。

换一个思路

先说一下init和new的区别:

复制代码代码如下:

classFoo(object):
   __instance=None
   def__init__(self):
       print"init"
if__name__=="__main__":
   foo=Foo()

运行结果是:
复制代码代码如下:
init
而下面的示例:
复制代码代码如下:
classFoo(object):
   __instance=None
   def__init__(self):
       print"init"
   def__new__(cls,*args,**kwargs):
       print"new"

if__name__=="__main__":
   foo=Foo()


运行结果是:
复制代码代码如下:new

new是一个类方法,会创建对象时调用。而init方法是在创建完对象后调用,对当前对象的实例做一些一些初始化,无返回值。如果重写了new而在new里面没有调用init或者没有返回实例,那么init将不起作用。以下内容引用自http://docs.python.org/2/reference/datamodel.html#object.new

复制代码代码如下:
If__new__()returnsaninstanceofcls,thenthenewinstance"s__init__()methodwillbeinvokedlike__init__(self[,...]),whereselfisthenewinstanceandtheremainingargumentsarethesameaswerepassedto__new__().

If__new__()doesnotreturnaninstanceofcls,thenthenewinstance"s__init__()methodwillnotbeinvoked.


这样做:
复制代码代码如下:
classFoo(object):
   __instance=None
   def__init__(self):
       print"init"

   def__new__(cls,*args,**kwargs):
       print"new"
       ifcls.__instance==None:
           cls.__instance=cls.__new__(cls,*args,**kwargs)
       returncls.__instance

if__name__=="__main__":
   foo=Foo()

   错误如下:

复制代码代码如下:
RuntimeError:maximumrecursiondepthexceededincmp

而这样也有一样的错误:

复制代码代码如下:
classFoo(object):
   __instance=None
   def__init__(self):
       ifself.__class__.__instance==None:
           self.__class__.__instance=Foo()
       print"init"

if__name__=="__main__":
   foo=Foo()


该怎么做呢?

下面参考了http://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python/31887#31887:

复制代码代码如下:
classFoo(object):
   __instance=None
   def__new__(cls,*args,**kwargs):
       print"hhhhhhhhh"
       ifnotcls.__instance:
           cls.__instance=super(Foo,cls).__new__(cls,*args,**kwargs)
       returncls.__instance

   defhi(self):
       print"hi,world"
       print"hi,letian"

if__name__=="__main__":
   foo1=Foo()
   foo2=Foo()
   printid(foo1)
   printid(foo2)
   printisinstance(foo1,object)
   printisinstance(foo1,Foo)
   foo1.hi()


运行结果:
复制代码代码如下:
hhhhhhhhh
hhhhhhhhh
39578896
39578896
True
True
hi,world
hi,letian
那么,到底发生了什么,我们先回顾一下super:

复制代码代码如下:
>>>printsuper.__doc__
super(type)->unboundsuperobject
super(type,obj)->boundsuperobject;requiresisinstance(obj,type)
super(type,type2)->boundsuperobject;requiresissubclass(type2,type)
Typicalusetocallacooperativesuperclassmethod:
classC(B):
   defmeth(self,arg):
       super(C,self).meth(arg)
可以肯定上面的单例模式代码中的这一行代码:
复制代码代码如下:
cls.__instance=super(Foo,cls).__new__(cls,*args,**kwargs)
super(Foo,cls)是object,super(Foo,cls).new方法使用的是object的new方法。我们看一下object.new方法的作用:
复制代码代码如下:
>>>printobject.__new__.__doc__
T.__new__(S,...)->anewobjectwithtypeS,asubtypeofT

如果是一个继承链

复制代码代码如下:
classFo(object):
   def__new__(cls,*args,**kwargs):
       print"hi,iamFo"
       return super(Fo,cls).__new__(cls,*args,**kwargs)

classFoo(Fo):
   __instance=None
   def__new__(cls,*args,**kwargs):
       ifnotcls.__instance:
           printFooiscls
           printissubclass(cls,Fo)
           printissubclass(cls,object)
           cls.__instance=super(Foo,cls).__new__(cls,*args,**kwargs)
       returncls.__instance

   defhi(self):
       print"hi,world"

if__name__=="__main__":
   foo1=Foo()
   foo1.hi()
   printisinstance(foo1,Foo)
   printisinstance(foo1,Fo)
   printisinstance(foo1,object)


运行结果如下:
复制代码代码如下:
True
True
True
hi,iamFo
hi,world
True
True
True
如果如下定义Fo,也正常运行:
复制代码代码如下:
classFo(object):
   pass
但是,若这样定义:
复制代码代码如下:
classFo(object):
   def__new__(cls,*args,**kwargs):
       print"hi,iamFo"
运行时报错如下:
复制代码代码如下:
AttributeError:"NoneType"objecthasnoattribute"hi"