[Add]新增用于实现单例模式的VtSingleton类,修改LogEngine的单例模式实现

This commit is contained in:
vn.py 2017-12-18 10:03:54 +08:00
parent 65bc568888
commit d006349fc3
3 changed files with 20 additions and 12 deletions

View File

@ -9,7 +9,7 @@
"darkStyle": true,
"language": "chinese",
"logActive": false,
"logActive": true,
"logLevel": "debug",
"logConsole": true,
"logFile": true,

View File

@ -538,6 +538,9 @@ class DataEngine(object):
class LogEngine(object):
"""日志引擎"""
# 单例模式
__metaclass__ = VtSingleton
# 日志级别
LEVEL_DEBUG = logging.DEBUG
LEVEL_INFO = logging.INFO
@ -545,16 +548,6 @@ class LogEngine(object):
LEVEL_ERROR = logging.ERROR
LEVEL_CRITICAL = logging.CRITICAL
# 单例对象
instance = None
#----------------------------------------------------------------------
def __new__(cls, *args, **kwargs):
"""创建对象,保证单例"""
if not cls.instance:
cls.instance = super(LogEngine, cls).__new__(cls, *args, **kwargs)
return cls.instance
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""

View File

@ -327,6 +327,21 @@ class VtCancelOrderReq(object):
self.sessionID = EMPTY_STRING # 会话号
########################################################################
class VtSingleton(type):
"""
单例应用方式:静态变量 __metaclass__ = Singleton
"""
_instances = {}
#----------------------------------------------------------------------
def __call__(cls, *args, **kwargs):
"""调用"""
if cls not in cls._instances:
cls._instances[cls] = super(VtSingleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]