技术篇–每日一篇0x4-python单例模式

1,398次阅读
没有评论

技术篇--每日一篇0x4-python单例模式

方法一:使用装饰器

装饰器维护一个字典对象instances,缓存了所有单例类,只要单例不存在则创建,已经存在直接返回该实例对象。

def singleton(cls):
    instances = {}
    #看到有人在这有疑问,为啥instances会有缓存功能?函数调用结束时所有变量不是释放?
    #正解:只有程序运行结束时退出才会释放所有资源,函数运行时会创建一个函数作用域,instances就是在当前函数
    #singleton作用域内,此处调用了两次,因此在第二次判断的时候instances里面缓存了上一次函数运行时产生的数据
    def wrapper(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]

    return wrapper


@singleton
class Foo(object):
    pass

foo1 = Foo()
foo2 = Foo()

print foo1 is foo2

方法二:使用基类

__new__是真正创建实例对象的方法,所以重写基类的__new__方法,以此来保证创建对象的时候只生成一个实例

<span class="k">class</span> <span class="n">Singleton</span>(<span class="n">object</span>):
    <span class="n">def</span> <span class="n">__new__</span>(<span class="n">cls</span>, *<span class="n">args</span>, **<span class="n">kwargs</span>):
        <span class="k">if</span> <span class="nb">not</span> <span class="n">hasattr</span>(<span class="n">cls</span>, <span class="s">'_instance'</span>):
            <span class="n">cls</span>.<span class="n">_instance</span> = <span class="n">super</span>(<span class="n">Singleton</span>, <span class="n">cls</span>).<span class="n">__new__</span>(<span class="n">cls</span>, *<span class="n">args</span>, **<span class="n">kwargs</span>)
        <span class="k">return</span> <span class="n">cls</span>.<span class="n">_instance</span>


<span class="k">class</span> <span class="n">Foo</span>(<span class="n">Singleton</span>):
    <span class="nb">pass</span>

<span class="n">foo1</span> = <span class="n">Foo</span>()
<span class="n">foo2</span> = <span class="n">Foo</span>()

<span class="nb">print</span> <span class="n">foo1</span> <span class="k">is</span> <span class="n">foo2</span>  <span class="c c-Singleline"># True</span>

方法三:使用元类

元类(参考:深刻理解Python中的元类)是用于创建类对象的类,类对象创建实例对象时一定会调用__call__方法,因此在调用__call__时候保证始终只创建一个实例即可,type是python中的一个元类。

<span class="k">class</span> <span class="n">Singleton</span>(<span class="n">type</span>):
    <span class="n">def</span> <span class="n">__call__</span>(<span class="n">cls</span>, *<span class="n">args</span>, **<span class="n">kwargs</span>):
        <span class="k">if</span> <span class="nb">not</span> <span class="n">hasattr</span>(<span class="n">cls</span>, <span class="s">'_instance'</span>):
            <span class="n">cls</span>.<span class="n">_instance</span> = <span class="n">super</span>(<span class="n">Singleton</span>, <span class="n">cls</span>).<span class="n">__call__</span>(*<span class="n">args</span>, **<span class="n">kwargs</span>)
        <span class="k">return</span> <span class="n">cls</span>.<span class="n">_instance</span>


<span class="k">class</span> <span class="n">Foo</span>(<span class="n">object</span>):
    <span class="n">__metaclass__</span> = <span class="n">Singleton</span>


<span class="n">foo1</span> = <span class="n">Foo</span>()
<span class="n">foo2</span> = <span class="n">Foo</span>()

<span class="nb">print</span> <span class="n">foo1</span> <span class="k">is</span> <span class="n">foo2</span>  <span class="c c-Singleline"># True</span>

转载自 https://foofish.net/python_singleton.html并且适当修改注释

admin
版权声明:本站原创文章,由admin2018-03-21发表,共计1135字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)