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 ...
随机推荐
- 在VS2010中使用Git(转)
在之前的一片博客<Windows 下使用Git管理Github项目>中简单介绍了在Windows环境中使用Git管理Github项目,但是是使用命令行来进行操作的,本文将简单介绍下在VS2 ...
- 界面上传文件js包【AjaxUpload.js】
function uploadFile() { new AjaxUpload($("#importFile"), { action: url, type: "POST&q ...
- git环境搭建
Linux kernel 的官方 GIT地址是: http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git 可以从这个地 ...
- Cadence ORCAD CAPTURE元件库介绍
Cadence ORCAD CAPTURE元件库介绍 来源:Cadence 作者:ORCAD 发布时间:2007-07-08 发表评论 Cadence OrCAD Capture 具有快捷.通用的 ...
- ACdream 1726 A Math game (dfs+二分)
http://acdream.info/problem?pid=1726 官方题解:http://acdream.info/topic?tid=4246 求n个数里面能不能选一些数出来让它们的和等于k ...
- BZOJ 2173 整数的lqp拆分
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2173 题意:给出输出n.设一种拆分为n=x1+x2+x3,那么这种拆分的价值为F(x1) ...
- C# treeview控件部分节点添加checkbox
一.先初始化treeview this.treeView1.CheckBoxes = true; this.treeView1.ShowLines = false; this.treeView1.Dr ...
- [HDOJ5521]Meeting(最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 给n个点,m个块.块内点到点之间话费的时间ti.两个人分别从点1和点n出发,问两人是否可以相遇, ...
- leetcode:Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 网易新闻页面信息抓取 -- htmlagilitypack搭配scrapysharp
最近在弄网页爬虫这方面的,上网看到关于htmlagilitypack搭配scrapysharp的文章,于是决定试一试~ 于是到https://www.nuget.org/packages/Scrapy ...