单例模式在某些场景下,我们希望实体类无论实例化多少次都只会产生一个实体对象,这时候就需要使用单例模式。经常使用的场景就是全局配置类。模式框架方式1:使用修饰器"““使用函数定义装饰器”““def singletons(cls): "”” 定义一个单例装饰器,使用dict保存定义好的实体,key为class的地址而不是名字,这样同名类也不会冲突 "”" instances = {} def wrapper(*args, **kwargs): if cls not in instances.keys(): instances[cls] = cls(*args, **kwargs) return instances[cls] return wrapper"““使用类定义装饰器”““class singletons(object): instances = {} def init(self, cls): self.__cls = cls def call(self, *args, **kwargs): if self.__cls not in singletons.instances.keys(): singletons.instances[self.__cls] = self.__cls(*args, **kwargs) return singletons.instances[self.__cls]方式2:重写__new__方法,只能针对当前修改的类有效class SingletonTest(object): __instance = None __isFirstInit = False def new(cls, name): if not cls.__instance: SingletonTest.__instance = super().new(cls) return cls.__instance def init(self, name): if not self.__isFirstInit: self.__name = name SingletonTest.__isFirstInit = True def getName(self): return self.__nameUML图示例@singletonsclass Test(object): def init(self, name): self.__name = name def hello(self): print(“I am {} object {}".format(self.__name, id(self)))if name == “main”: test1 = Test(“test1”) test2 = Test(“test2”) test1.hello() test2.hello()‘‘‘测试输出’’’# I am test1 object 2453169112512# I am test1 object 2453169112512扩展思考单例模式的线程安全问题。