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


Posted in Python onApril 26, 2014

注:使用的是Python 2.7。

一个简单实现

class Foo(object):
    __instance = None
    def __init__(self):
        pass
    @classmethod
    def getinstance(cls):
        if(cls.__instance == None):
            cls.__instance = Foo()
        return cls.__instance
if __name__ == '__main__':
    foo1 = Foo.getinstance()
    foo2 = Foo.getinstance()
    print id(foo1)
    print id(foo2)
    print id(Foo())

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

换一个思路

先说一下init和new的区别:

class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'
if __name__ == '__main__':
    foo = Foo()

运行结果是:
init

而下面的示例:
class Foo(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__() returns an instance of cls, then the new instance's __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().
If __new__() does not return an instance of cls, then the new instance's __init__() method will not be invoked.

这样做:
class Foo(object):
    __instance = None
    def __init__(self):
        print 'init'
    def __new__(cls, *args, **kwargs):
        print 'new'
        if cls.__instance == None:
            cls.__instance = cls.__new__(cls, *args, **kwargs)
        return cls.__instance
if __name__ == '__main__':
    foo = Foo()

    错误如下:

RuntimeError: maximum recursion depth exceeded in cmp

而这样也有一样的错误:

class Foo(object):
    __instance = None
    def __init__(self):
        if self.__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:

class Foo(object):
    __instance = None
    def __new__(cls, *args, **kwargs):
        print 'hhhhhhhhh'
        if not cls.__instance:
            cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return cls.__instance
    def hi(self):
        print 'hi, world'
        print 'hi, letian'
if __name__ == '__main__':
    foo1 = Foo()
    foo2 = Foo()
    print id(foo1)
    print id(foo2)
    print isinstance(foo1, object)
    print isinstance(foo1, Foo)
    foo1.hi()

运行结果:
hhhhhhhhh
hhhhhhhhh
39578896
39578896
True
True
hi, world
hi, letian

那么,到底发生了什么,我们先回顾一下super:
>>> print super.__doc__
super(type) -> unbound super object
super(type, obj) -> bound super object; requires isinstance(obj, type)
super(type, type2) -> bound super object; requires issubclass(type2, type)
Typical use to call a cooperative superclass method:
class C(B):
    def meth(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方法的作用:
>>> print object.__new__.__doc__
T.__new__(S, ...) -> a new object with type S, a subtype of T

如果是一个继承链

class Fo(object):
    def __new__(cls, *args, **kwargs):
        print 'hi, i am Fo'
        return  super(Fo, cls).__new__(cls, *args, **kwargs)
class Foo(Fo):
    __instance = None
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            print Foo is cls
            print issubclass(cls, Fo)
            print issubclass(cls, object)
            cls.__instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return cls.__instance
    def hi(self):
        print 'hi, world'
if __name__ == '__main__':
    foo1 = Foo()
    foo1.hi()
    print isinstance(foo1, Foo)
    print isinstance(foo1, Fo)
    print isinstance(foo1, object)

运行结果如下:
True
True
True
hi, i am Fo
hi, world
True
True
True

如果如下定义Fo,也正常运行:
class Fo(object):
    pass

但是,若这样定义:
class Fo(object):
    def __new__(cls, *args, **kwargs):
        print 'hi, i am Fo'

运行时报错如下:
AttributeError: 'NoneType' object has no attribute 'hi'
Python 相关文章推荐
python del()函数用法
Mar 24 Python
Python3读取zip文件信息的方法
May 22 Python
python实现的用于搜索文件并进行内容替换的类实例
Jun 28 Python
Python爬虫之pandas基本安装与使用方法示例
Aug 08 Python
Python后台管理员管理前台会员信息的讲解
Jan 28 Python
pytorch之inception_v3的实现案例
Jan 06 Python
tensorflow 实现数据类型转换
Feb 17 Python
python tkinter GUI绘制,以及点击更新显示图片代码
Mar 14 Python
python实现磁盘日志清理的示例
Nov 05 Python
利用python+ffmpeg合并B站视频及格式转换的实例代码
Nov 24 Python
Python实现Kerberos用户的增删改查操作
Dec 14 Python
详解Python中*args和**kwargs的使用
Apr 07 Python
Python设计模式之观察者模式实例
Apr 26 #Python
Python设计模式之代理模式实例
Apr 26 #Python
python中的列表推导浅析
Apr 26 #Python
Python中的Numpy入门教程
Apr 26 #Python
Python中的map、reduce和filter浅析
Apr 26 #Python
Python实现的Kmeans++算法实例
Apr 26 #Python
爬山算法简介和Python实现实例
Apr 26 #Python
You might like
php使用cookie显示用户上次访问网站日期的方法
2015/01/26 PHP
php实现图片上传并利用ImageMagick生成缩略图
2016/03/14 PHP
php 使用ActiveMQ发送消息,与处理消息操作示例
2020/02/23 PHP
Javascript笔记一 js以及json基础使用说明
2010/05/22 Javascript
jquery下jstree简单应用 - v1.0
2011/04/14 Javascript
JS+flash实现chrome和ie浏览器下同时可以复制粘贴
2013/09/22 Javascript
Javascript之this关键字深入解析
2013/11/12 Javascript
浅谈js中变量初始化
2015/02/03 Javascript
JavaScript获取页面中表单(form)数量的方法
2015/04/03 Javascript
js实现表单检测及表单提示的方法
2015/08/14 Javascript
javascript ASCII和Hex互转的实现方法
2016/12/27 Javascript
vue发送ajax请求详解
2018/10/09 Javascript
Layui 解决表格异步调用后台分页的问题
2019/10/26 Javascript
用jQuery实现抽奖程序
2020/04/12 jQuery
js实现金山打字通小游戏
2020/07/24 Javascript
uniapp微信小程序实现一个页面多个倒计时
2020/11/01 Javascript
Python Socket实现简单TCP Server/client功能示例
2017/08/05 Python
Python之Scrapy爬虫框架安装及简单使用详解
2017/12/22 Python
python SMTP实现发送带附件电子邮件
2018/05/22 Python
Python中捕获键盘的方式详解
2019/03/28 Python
利用 Flask 动态展示 Pyecharts 图表数据方法小结
2019/09/04 Python
python 如何读、写、解析CSV文件
2021/03/03 Python
波兰最大的度假胜地和城市公寓租赁运营商:Sun & Snow
2018/10/18 全球购物
说说你所熟悉或听说过的j2ee中的几种常用模式?及对设计模式的一些看法
2012/05/24 面试题
家居饰品店创业计划书
2014/01/31 职场文书
运动会广播稿20字
2014/02/18 职场文书
小学三年级学生评语
2014/04/22 职场文书
管理岗位竞聘演讲稿
2014/08/18 职场文书
学校领导班子群众路线整改措施
2014/09/16 职场文书
北京离婚协议书范文2014
2014/09/29 职场文书
群众路线专项整治方案
2014/10/27 职场文书
2014年信访工作总结
2014/11/17 职场文书
一年级数学上册复习计划
2015/01/17 职场文书
2015年办公室主任工作总结
2015/04/09 职场文书
2015年预防青少年违法犯罪工作总结
2015/05/22 职场文书
《称赞》教学反思
2016/02/17 职场文书