一、什么是单例

即单个实例,指的是同一个类实例化多次的结果指向同一个对象,用于节省内存空间

如果我们从配置文件中读取配置来进行实例化,在配置相同的情况下,就没必要重复产生对象浪费内存了

# settings.py文件
IP = '1.1.1.1'
PORT = 3303

二、四种单例模式的实现方式

# 方式一:定义一个类方法实现单例模式

import settings

class People:
__instance = None #用于保存实例化的状态 def __init__(self, ip, port):
self.ip = ip
self.port = port @classmethod
def get(cls):
if cls.__instance is None:
cls.__instance = cls(settings.IP, settings.PORT)
return cls.__instance obj1 = People.get()
obj2 = People.get()
print(obj1)
print(obj2)
# <__main__.People object at 0x00000000021BA9B0>
# <__main__.People object at 0x00000000021BA9B0> #方式二:定义一个装饰器实现单例模式
import settings def singlet(cls):
_instance = cls(settings.IP, settings.PORT) #先实例化一个对象 def wrapper(*args, **kwargs):
if args or kwargs:
obj = cls(*args, **kwargs)
return obj #有参数是返回后来实例化对象
return _instance #无参时,返回已经实例化好的对象 return wrapper @singlet # MySQL=wrapper(MySQL)
class MySQL: def __init__(self, ip, port):
self.ip = ip
self.port = port # 没有参数时,单例模式
obj1 = MySQL()
obj2 = MySQL()
obj3 = MySQL()
obj4 = MySQL('1.1.1.1', 3303)
print(obj1)
print(obj2)
print(obj3)
print(obj4)
# <__main__.MySQL object at 0x0000000001E6AA90>
# <__main__.MySQL object at 0x0000000001E6AA90>
# <__main__.MySQL object at 0x0000000001E6AA90>
# <__main__.MySQL object at 0x0000000001E6AB00> #方式三:定制元类实现单例模式
import settings class Mymeta(type):
def __init__(self, class_name, class_bases, class_dic):
instance = self(settings.IP, settings.PORT)
self.__instance = instance def __call__(self, *args, **kwargs): # self=MySQL
if args or kwargs: # 有参数执行下面的代码
obj = self.__new__(self) #创造一个空对象
self.__init__(obj, *args, **kwargs) #初始化
return obj #返回对象
return self.__instance class MySQL(metaclass=Mymeta):
def __init__(self, ip, port):
self.ip = ip
self.port = port obj1 = MySQL()
obj2 = MySQL()
obj3 = MySQL()
obj4 = MySQL('1.2.3.1', 3305)
print(obj1)
print(obj2)
print(obj3)
print(obj4)
# <__main__.MySQL object at 0x00000000021BAA90>
# <__main__.MySQL object at 0x00000000021BAA90>
# <__main__.MySQL object at 0x00000000021BAA90>
# <__main__.MySQL object at 0x00000000021BAB00> 方式四:利用模块导入实现单例模式
# singleton模块
import settings class MySQL:
def __init__(self, ip, port):
self.ip = ip
self.port = port instance = MySQL(settings.IP, settings.PORT) #单例模块
def f1():
from singleton import instance
print(instance) def f2():
from singleton import instance, MySQL
print(instance)
obj = MySQL('', 3302)
print(obj) f1()
f2() # <singleton.MySQL object at 0x00000000021FAA90>
# <singleton.MySQL object at 0x00000000021FAA90>
# <singleton.MySQL object at 0x00000000021FA358>

最新文章

  1. Console.In.ReadToEnd() 控制台 输入完毕
  2. ASP.NET MVC Razor HtmlHelper扩展和自定义控件
  3. 信息安全系统设计基础exp_2
  4. jquery获取第几个元素的方法总结
  5. 文件夹工具类 - FolderUtils
  6. Robotium学习笔记一
  7. C++库编译
  8. phpcms v9自定义表单提交后返回上一页实现方法
  9. leetcode Valid Parentheses python
  10. mahout安装和测试
  11. 详解一下网络广告cpc、cpm、cpl、cpa、cps、cpr的计费方法是什么
  12. Web网页数据抓取(C/S)
  13. 安卓和IOS兼容问题
  14. 择天记OL体验截图
  15. Promise源码深入理解
  16. UML-Based Modeling of Robustness Testing
  17. 【原创】Linux基础之命令行浏览器links
  18. Day 14 三元运算符,列表推导式,内置函数
  19. css 解决fixed 布局下不能滚动的问题
  20. model number

热门文章

  1. K12
  2. [LeetCode] 51. N-Queens N皇后问题
  3. 《TP5.0学习笔记---模板变量输出、替换和赋值篇》
  4. Windows下MySQL安装流程,8.0以上版本ROOT密码报错及修改
  5. jsp之el表达式jstl标签
  6. linux 成功安装oracle后,为其创建一个登录账户
  7. Laravel本地环境搭建:Homestead开发环境的部署
  8. unityUIMask
  9. ros:init()
  10. WebStrom安装Markdown插件