最近实现了一个单体模式,来处理一个基于JSON数据结构的任务,发出来希望高手指点。
代码片段
# -*-coding:utf-8-*-
# version:0.2
# 重构,以方便实现在table中增加一行的功能
# simple task manager
import sys
import json
"""
为Task单独实现一个类,这个类就是用来存储数据的,并且只有一份,也就是单体模式;
"""
class TasksSingleton(object):
_instance = None
_taskJson = None
_taskFileName = None
"""为了实现单体的方法,继承object类,重构其__new__函数"""
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls.loadFile()
return cls._instance
"""获得当前应该使用的task数据,例如9月份的数据为task9.json"""
@staticmethod
def getFileName():
return "./taskdata/tasks9.json"
"""将文件系统中的jason文件,导入到TasksSingleton类的_taskJson参数中"""
@staticmethod
def loadFile():
try: