Python单例模式研究
方法一
- import threading
- class Singleton(object):
- __instance = None
- __lock = threading.Lock() # used to synchronize code
- def __init__(self):
- "disable the __init__ method"
- @staticmethod
- def getInstance():
- if not Singleton.__instance:
- Singleton.__lock.acquire()
- if not Singleton.__instance:
- Singleton.__instance = object.__new__(Singleton)
- object.__init__(Singleton.__instance)
- Singleton.__lock.release()
- return Singleton.__instance
1.禁用__init__方法,不能直接创建对象。
2.__instance,单例对象私有化。
3.@staticmethod,静态方法,通过类名直接调用。
4.__lock,代码锁。
5.继承object类,通过调用object的__new__方法创建单例对象,然后调用object的__init__方法完整初始化。
6.双重检查加锁,既可实现线程安全,又使性能不受很大影响。
方法二:使用decorator
- #encoding=utf-8
- def singleton(cls):
- instances = {}
- def getInstance():
- if cls not in instances:
- instances[cls] = cls()
- return instances[cls]
- return getInstance
- @singleton
- class SingletonClass:
- pass
- if __name__ == '__main__':
- s = SingletonClass()
- s2 = SingletonClass()
- print s
- print s2
也应该加上线程安全
附:性能没有方法一高
- import threading
- class Sing(object):
- def __init__():
- "disable the __init__ method"
- __inst = None # make it so-called private
- __lock = threading.Lock() # used to synchronize code
- @staticmethod
- def getInst():
- Sing.__lock.acquire()
- if not Sing.__inst:
- Sing.__inst = object.__new__(Sing)
- object.__init__(Sing.__inst)
- Sing.__lock.release()
- return Sing.__inst
Python单例模式研究的更多相关文章
- python 单例模式获取IP代理
python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...
- Python 单例模式讲解
Python 单例模式讲解 本节内容: classmethod用途 单例模式方法一 类__new__方法讲解 单例模式方法二 前言: 使用单例方法的好处:对于一个类,多次实例化会产生多个对象,若使用单 ...
- python单例模式的实现与优化
python单例模式的实现与优化 阅读目录(Content) 单例模式 实现单例模式的几种方式 1.使用模块 2.使用装饰器 3.使用类 4.基于__new__方法实现(推荐使用,方便) 5.基于me ...
- Python单例模式的设计与实现【完美版】
目录 1. 按 2. 本文地址 3. 通过继承单例父类来实现 4. 使用装饰器实现 4.1. 懒汉式 4.2. 饿汉式 4.2.1. 未加锁版 4.2.2. 加锁版 1. 按 众所周知,对象是解决继承 ...
- Python 单例模式的几种实现方式
单例模式的几种实现方式 先来看几个魔法方法的简单运用:__new__, __init__, __call__. class A(object): def __init__(self, x): prin ...
- python 单例模式
单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对象的类必须保证只有一个实例存在 用装饰器方式实现单例模式 #!/usr/bin/python # coding=utf-8 d ...
- Python单例模式
1.单例模式介绍 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时, 单例对象的类必须保证只有一个实例存在.许多时候整个系统只需要拥有一个 全局对象,这样有利于我们协调系统整体的行为 ...
- python 单例模式的四种创建方式
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- python 单例模式的四种实现方法及注意事项
一.模块单例 Python 的模块就是天然的单例模式,因为模块在第一次导入时,会生成 .pyc 文件,当第二次导入时,就会直接加载 .pyc 文件,而不会再次执行模块代码. #foo1.py clas ...
随机推荐
- JSP列表形式显示数据库中的数据 OracleCachedRowSet 实例
现在数据库中有一张用户表,希望用户在jsp页面中输入用户名和密码以及 用户类型,在servlet中插入数据库后,在另一个jsp页面中把数据库中所有的用户名和类型都以列表的形式列出来 可以用Ora ...
- linux select
man select: #include <sys/select.h> #include <sys/time.h> int select(int nfds, fd_set *r ...
- 通过xrdp实现远程桌面连接Windows Azure linux虚拟机
本文以Oracle Linux 6.4虚拟机为示例(22及3389端口必须打开,分别用于SSH及RDP连接) 1.在安装xrdp之前,首先需要安装一些必要的包,如: # yum -y install ...
- 干货:yii日志功能详解
转载请注明来自souldak,微博:@evagle 一.基本日志功能 详细的介绍查看官网的document:http://www.yiiframework.com/doc/guide/1.1/en/t ...
- KVM/QEMU桥接网络设置及kvm资料
KVM/QEMU桥接网络设置 配置kvm的网络有2种方法.其一,默认方式为用户模式网络(Usermode Networking),数据包由NAT方式通过主机的接口进行传送.其二,使用桥接方式(Brid ...
- POJ-3187 Backward Digit Sums (暴力枚举)
http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...
- C# treeview控件部分节点添加checkbox
一.先初始化treeview this.treeView1.CheckBoxes = true; this.treeView1.ShowLines = false; this.treeView1.Dr ...
- 宏ut_mem_block_t
/** This struct is placed first in every allocated memory block */ typedef struct ut_mem_block_struc ...
- DELPHI 里面的迭代
迭代(Iiterator)的作用:遍历一个集合(Collections)的每一个元素(item). delphi 2005之后新加入一种 for .. in .. 遍历语句,支持String,Set, ...
- UVA 11383 Golden Tiger Claw(最佳二分图完美匹配)
题意:在一个N*N的方格中,各有一个整数w(i,j),现在要求给每行构造row(i),给每列构造col(j),使得任意w(i,j)<=row(i)+col(j),输出row(i)与col(j)之 ...