单例设计模式详细操作教程
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
class Singleton:
__instance = None
@staticmethod
def getInstance():
""" Static access method. """
if Singleton.__instance == None:
Singleton()
return Singleton.__instance
def __init__(self):
""" Virtually private constructor. """
if Singleton.__instance != None:
raise Exception("This class is a singleton!")
else:
Singleton.__instance = self
s = Singleton()
print s
s = Singleton.getInstance()
print s
s = Singleton.getInstance()
print s
<__main__.Singleton object at 0x10ee9a430>
<__main__.Singleton object at 0x10ee9a430>
<__main__.Singleton object at 0x10ee9a430>