網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
簡(jiǎn)介:?jiǎn)卫J娇梢员WC一個(gè)類僅有一個(gè)實(shí)例,并提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)。適用性于當(dāng)類只能有一個(gè)實(shí)例而且客戶可以從一個(gè)眾所周知的訪問(wèn)點(diǎn)訪問(wèn)它,例如訪問(wèn)數(shù)據(jù)庫(kù)、MQ等。
實(shí)現(xiàn)方式:
1、通過(guò)導(dǎo)入模塊實(shí)現(xiàn)
2、通過(guò)裝飾器實(shí)現(xiàn)
3、通過(guò)使用類實(shí)現(xiàn)
4、通過(guò)__new__ 方法實(shí)現(xiàn)
單例模塊方式被導(dǎo)入的源碼:singleton.py
# -*- coding: utf-8 -*-
# time: 2022/5/17 10:31
# file: singleton.py
# author: tom
# 公眾號(hào): 玩轉(zhuǎn)測(cè)試開發(fā)
class Singleton(object):
def __init__(self, name):
self.name = name
def run(self):
print(self.name)
s = Singleton("Tom")
主函數(shù)源碼:
# -*- coding: utf-8 -*-
# time: 2022/5/17 10:51
# file: test_singleton.py
# author: tom
# 公眾號(hào): 玩轉(zhuǎn)測(cè)試開發(fā)
from singleton import s as s1
from singleton import s as s2
# Method One:通過(guò)導(dǎo)入模塊實(shí)現(xiàn)
def show_method_one():
"""
:return:
"""
print(s1)
print(s2)
print(id(s1))
print(id(s2))
show_method_one()
# Method Two:通過(guò)裝飾器實(shí)現(xiàn)
def singleton(cls):
# 創(chuàng)建一個(gè)字典用來(lái)保存類的實(shí)例對(duì)象
_instance = {}
def _singleton(*args, **kwargs):
# 先判斷這個(gè)類有沒(méi)有對(duì)象
if cls not in _instance:
_instance[cls] = cls(*args, **kwargs) # 創(chuàng)建一個(gè)對(duì)象,并保存到字典當(dāng)中
# 將實(shí)例對(duì)象返回
return _instance[cls]
return _singleton
@singleton
class Demo2(object):
a = 1
def __init__(self, x=0):
self.x = x
a1 = Demo2(1)
a2 = Demo2(2)
print(id(a1))
print(id(a2))
# Method Three:通過(guò)使用類實(shí)現(xiàn)
class Demo3(object):
# 靜態(tài)變量
_instance = None
_flag = False
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
if not Demo3._flag:
Demo3._flag = True
b1 = Demo3()
b2 = Demo3()
print(id(b1))
print(id(b2))
# Method Four:通過(guò)__new__ 方法實(shí)現(xiàn)
class Demo4:
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = super(Demo4, cls).__new__(cls)
return cls._instance
c1 = Demo4()
c2 = Demo4()
print(id(c1))
print(id(c2))
運(yùn)行結(jié)果:
原文鏈接:https://blog.csdn.net/hzblucky1314/article/details/124833122
相關(guān)推薦
- 2022-08-19 mv命令linux
- 2022-12-27 詳解Golang中interface接口的原理和使用技巧_Golang
- 2023-07-07 什么是依賴注入?可以通過(guò)多少種方式完成依賴注入?
- 2022-05-23 python列表排序用?sort()和sorted()的區(qū)別_python
- 2022-07-09 kubernetes之證書更新
- 2023-10-17 el-table-column 表單table的后端返回時(shí)間戳的轉(zhuǎn)換
- 2023-12-12 Mybatis中一些優(yōu)化
- 2022-11-14 mq消息積壓怎么對(duì)應(yīng)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支