单例模式的实现方式

将类实例绑定到类变量上

class Singleton(object):

_instance = None

def new(cls, *args):

if not isinstance(cls._instance, cls):

cls._instance = super(Singleton, cls).new(cls, *args)

return cls._instance

但是子类在继承后可以重写__new__以失去单例特性

class D(Singleton):

def new(cls, *args):

return super(D, cls).new(cls, *args)

使用装饰器实现

def singleton(_cls):

inst = {}

def getinstance(args, **kwargs):

if _cls not in inst:

inst[_cls] = _cls(
args, **kwargs)

return inst[_cls]

return getinstance

@singleton

class MyClass(object):

pass

问题是这样装饰以后返回的不是类而是函数,当然你可以singleton里定义一个类来解决问题,但这样就显得很麻烦了

使用__metaclass__,这个方式最推荐

class Singleton(type):

_inst = {}

def call(cls, args, **kwargs):

if cls not in cls._inst:

cls._inst[cls] = super(Singleton, cls).call(
args)

return cls._inst[cls]

class MyClass(object):

metaclass = Singleton

Tornado中的单例模式运用

来看看tornado.IOLoop中的单例模式:

class IOLoop(object):

@staticmethod

def instance():

"""Returns a global IOLoop instance.

Most applications have a single, global IOLoop running on the

main thread. Use this method to get this instance from

another thread. To get the current thread's IOLoop, use current().

"""

if not hasattr(IOLoop, "_instance"):

with IOLoop._instance_lock:

if not hasattr(IOLoop, "_instance"):

# New instance after double check

IOLoop._instance = IOLoop()

return IOLoop._instance

为什么这里要double check?来看个这里面简单的单例模式,先来看看代码:

class Singleton(object):

@staticmathod

def instance():

if not hasattr(Singleton, '_instance'):

Singleton._instance = Singleton()

return Singleton._instance

在 Python 里,可以在真正的构造函数__new__里做文章:

class Singleton(object):

def new(cls, *args, **kwargs):

if not hasattr(cls, '_instance'):

cls._instance = super(Singleton, cls).new(cls, *args, **kwargs)

return cls._instance

这种情况看似还不错,但是不能保证在多线程的环境下仍然好用,看图:

201632180733229.png (683×463)

出现了多线程之后,这明显就是行不通的。

1.上锁使线程同步

上锁后的代码:

import threading

class Singleton(object):

_instance_lock = threading.Lock()

@staticmethod

def instance():

with Singleton._instance_lock:

if not hasattr(Singleton, '_instance'):

Singleton._instance = Singleton()

return Singleton._instance

这里确实是解决了多线程的情况,但是我们只有实例化的时候需要上锁,其它时候Singleton._instance已经存在了,不需要锁了,但是这时候其它要获得Singleton实例的线程还是必须等待,锁的存在明显降低了效率,有性能损耗。

2.全局变量

在 Java/C++ 这些语言里还可以利用全局变量的方式解决上面那种加锁(同步)带来的问题:

class Singleton {

private static Singleton instance = new Singleton();

private Singleton() {}

public static Singleton getInstance() {

return instance;

}

}

在 Python 里就是这样了:

class Singleton(object):

@staticmethod

def instance():

return _g_singleton

_g_singleton = Singleton()

def get_instance():

return _g_singleton

但是如果这个类所占的资源较多的话,还没有用这个实例就已经存在了,是非常不划算的,Python 代码也略显丑陋……

所以出现了像tornado.IOLoop.instance()那样的double check的单例模式了。在多线程的情况下,既没有同步(加锁)带来的性能下降,也没有全局变量直接实例化带来的资源浪费。

3.装饰器

如果使用装饰器,那么将会是这样:

import functools

def singleton(cls):

''' Use class as singleton. '''

cls.new_original = cls.new

@functools.wraps(cls.new)

def singleton_new(cls, *args, **kw):

it = cls.dict.get('it')

if it is not None:

return it

cls.__it__ = it = cls.__new_original__(cls, *args, **kw)
it.__init_original__(*args, **kw)
return it

cls.new = singleton_new

cls.init_original = cls.init

cls.init = object.init

return cls

Sample use:

@singleton

class Foo:

def new(cls):

cls.x = 10

return object.new(cls)

def init(self):

assert self.x == 10

self.x = 15

assert Foo().x == 15

Foo().x = 20

assert Foo().x == 20

def singleton(cls):

instance = cls()

instance.call = lambda: instance

return instance

Sample use

@singleton

class Highlander:

x = 100

Of course you can have any attributes or methods you like.

Highlander() is Highlander() is Highlander #=> True

id(Highlander()) == id(Highlander) #=> True

Highlander().x == Highlander.x == 100 #=> True

Highlander.x = 50

Highlander().x == Highlander.x == 50 #=> True

Python设计模式中单例模式的实现及在Tornado中的应用的更多相关文章

  1. python设计模式之单例模式(一)

    前言 单例模式是创建模式中比较常见和常用的模式,在程序执行的整个生命周期只存在一个实例对象. 系列文章 python设计模式之单例模式(一) python设计模式之常用创建模式总结(二) python ...

  2. python设计模式之单例模式(二)

    上次我们简单了解了一下什么是单例模式,今天我们继续探究.上次的内容点这 python设计模式之单例模式(一) 上次们讨论的是GoF的单例设计模式,该模式是指:一个类有且只有一个对象.通常我们需要的是让 ...

  3. python设计模式之单例模式(转)

    设计模式之单例模式 单例设计模式是怎么来的?在面向对象的程序设计中,当业务并发量非常大时,那么就会出现重复创建相同的对象,每创建一个对象就会开辟一块内存空间,而这些对象其实是一模一样的,那么有没有办法 ...

  4. python设计模式之单例模式(一)

    单例设计模式的概念: 单例设计模式即确保类有且只有一个特定类型的对象,并提供全局访问点.一般我们操作数据库的时候为了避免统一资源产生互相冲突,创建单例模式可以维护数据的唯一性. 单例模式的特性: 确保 ...

  5. Python设计模式之单例模式

    1.由于语言的特性不同,设计模式的实现方式和实现难度也会不同 2.有的模式已经在语言内置了,比如迭代器模式. 3.单例模式可以直接用模块级变量来实现 4.普通工厂模式可以直接通过传入"类名& ...

  6. 【python 设计模式】单例模式

    单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. 比如,某 ...

  7. python 设计模式之 单例模式

    单例模式是做为"全局变量"的替代品出现的.所以它具有全局变量的特点:全局可见.贯穿应用程序的整个生命期,保证在程序执行中,某个类仅仅存在一个实例,所以通常不希望类中的构造函数被调用 ...

  8. python 设计模式之单例模式 Singleton Pattern

    #引入 一个类被设计出来,就意味着它具有某种行为(方法),属性(成员变量).一般情况下,当我们想使用这个类时,会使用new 关键字,这时候jvm会帮我们构造一个该类的实例.这么做会比较耗费资源. 如果 ...

  9. python设计模式之--单例模式

    python的单例模式就是一个类的实例只能自始自终自能创建一次.应用场景比如说数据库的连接池. #!/usr/bin/env python # coding=utf- class Foo(object ...

随机推荐

  1. C 类网络的子网快速划分

    CIDR ( Classless Inter-Domain Routing ,无类域间路由选择) 进行子网划分的方法有很多,最适合你的方式就是正确的方式.在 C 类地址中,只有 8 位用于定义主机.注 ...

  2. Android Native jni 编程 Android.mk 文件编写

    LOCAL_PATH 必须位于Android.mk文件的最开始.它是用来定位源文件的位置,$(call my-dir)的作用就是返回当前目录的路径. LOCAL_PATH := $(call my-d ...

  3. 常州day1p4

    给定一棵 n 个点的树,树上每个节点代表一个小写字母,询问一个字符串 S 是否在树上出现过? 字符串 S 出现过即表示存在两个点 u,v,u 到 v 的最短路径上依次连接所有点上的字母恰好是 S N ...

  4. 【BZOJ1923】外星千足虫(线性基)

    [BZOJ1923]外星千足虫(线性基) 题面 BZOJ 洛谷 Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用"点足 ...

  5. Extjs treePanel过滤查询功能【转】

    Extjs4.2中,对于treeStore中未实现filterBy函数进行实现,treestore并未继承与Ext.data.Store,对于treePanel的过滤查询功能,可有以下两种实现思路: ...

  6. Codeforces Round #408 (Div. 2) A B C 模拟 模拟 set

    A. Buying A House time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Hdu1255 覆盖的面积

    覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. libiop网络库数据结构和基础知识

    最近朋友推荐,学习了libiop这个网络库,作者封装的很全面,代码很简洁 适合初学者学习基于事件驱动的网络io 先看看iop_def.h, 这里面定义了常用的数据结构 tag_iop_base_t 主 ...

  9. 题解【luogu3709 大爷的字符串题】

    Description 个人觉得这是这道题最难的一步...出题人的语文... 每次给出一个区间,求这个区间最少能被多少个单调上升的序列覆盖. Solution 这个东西可以转化为这个区间中出现次数最多 ...

  10. git相关网址

    git入门教程: 廖雪峰的官方网站 https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...