单例模式
=============
"""
单例模式:
        常用的软件设计模式,他的目的是为了保证某一个类只能有一个实例对象存在
比如,我们在写代码过程中,用同一个类产生很多个对象,而这些对象做的事情,只是为了执行一个或者某几个功能,
    这种频繁创建或者销毁的对象的过程,就会特别浪费内存资源
特点:
    只允许内存中有一个实例,减少内存的消耗
实现方法 :6种
1、模块
2、类装饰器
3、类绑定方法
4、__new__方法
5、元类
6、并发
用的最多的是第一种
"""
#单例模式 -- 模块实现
# class Human:
#     def __init__(self,name,age):
#         self.name =name
#         self.age =age
# obj = Human("张浩",23)
#把上面代码单独放到一个模块里面,我这里放在setting里面
# 通过导包拿过来
# from setting import 就可以了
# 这样不管后面用到多少,都是同一个对象,所以python模块就是一个天然的对象
#单例模式 -- 类装饰器
# def Class_decorators(fun):
#     # 先定义一个obj = None
#     response = None
#     # 在下面def做判断
#     def wapper(*args,**kwargs):
#         nonlocal response#因为我要让局部空间的response下面def空间的response,所以用nonlocal
#         if not response:#如果response为空,就产生一个对象
#             response = fun(*args,**kwargs)
#         return response
#     return wapper
#
# # @Class_decorators #Human = Class_decorators(Human)
# class Human:
#     def __init__(self,name,age):
#         self.name =name
#         self.age =age
# obj = Human("张浩",23)#我这里obj就是上面类装饰器的返回值(wapper)
# #后面继续调用,那就是判断下  response 是否为空
# print(obj)#<__main__.Human object at 0x00000216123F73D0>
#
# objs = Human("张浩",25)
# print(objs)#<__main__.Human object at 0x00000216123F73D0>
#两个是一个的返回值
# 装饰器去掉运行,可以看到就是两个不同的对象的
'''
<__main__.Human object at 0x000002A467536FB0>
<__main__.Human object at 0x000002A467537070>
'''
#单例模式 -- 类绑定方法
# class Human:
#     obj= None #定义一个类属性,默认为None
#     def __init__(self,name,age):
#         self.name =name
#         self.age =age
#     @classmethod #定个类方法
#     def get_obj(cls,*args,**kwargs):#cls代表自己,我需要什么参数也要接收
#         if not cls.obj:#判断类属性 obj是否为空
#             cls.obj = cls(*args,**kwargs)
#         return cls.obj #返回自己的obj属性
# # 下面想要实现单例模式也要调用里面的 get_obj(cls,*args,**kwargs)方法
#
# obj = Human.get_obj("账号",23)
# print(obj)#<__main__.Human object at 0x00000217906373D0>
# obj1 = Human.get_obj("账号",25)
# print(obj1)#<__main__.Human object at 0x00000217906373D0>
#单例模式 -- __new__方法
# class Human:
#     obj= None #定义一个类属性,默认为None
#     def __init__(self,name,age):
#         self.name =name
#         self.age =age
#
#     def __new__(self, *args, **kwargs):
#         if not self.obj:
#             # 这里不能直接self()调用,因为类+()会调用自己的 __call__方法,__call__方法又会调用当前类的__new__
#             # 就成了无线递归了
#             # self.obj = self(*args,**kwargs)
#             self.obj = super().__new__(self)#调用自己的父类
#         return self.obj
#
# obj = Human("账号",23)
# print(obj)#<__main__.Human object at 0x00000217906373D0>
# obj1 = Human("账号",25)
# print(obj1)#<__main__.Human object at 0x00000217906373D0>
#单例模式 -- 元类
# class Mytype(type):
#     obj = None
#     def __call__(self, *args, **kwargs):
#         if not self.obj:
#             self.obj = self.__new__(self)
#         self.__init__(self.obj,*args,**kwargs)
#         return self.obj
#
# class Human(metaclass=Mytype):
#     obj= None
#     def __init__(self,name,age):
#         self.name =name
#         self.age =age
#
# obj = Human("账号",23)
# print(obj.__dict__)#{'name': '账号', 'age': 23}
# print(obj)#<__main__.Human object at 0x00000274F886BFD0>
# obj1 = Human("账号",25)
# print(obj.__dict__)#{'name': '账号', 'age': 25}
# print(obj1)#<__main__.Human object at 0x00000274F886BFD0>
#可以看到,两个字典不一样,凡是类空间地址一样,这就是把
# self.__init__(self.obj,*args,**kwargs)
# 放在外面的结果
# 如果我们前面定义很多类,每次都要继承 Mytype太麻烦
class Mytype(type):
    obj = None
    def __call__(self, *args, **kwargs):
        if not self.obj:
            self.obj = self.__new__(self)
        self.__init__(self.obj,*args,**kwargs)
        return self.obj
class Singleton(metaclass=Mytype):
    pass
# 定义一个父类来用自定义元类,他的子孙类就都是用这个自定义元类
class Human(Singleton):
    obj= None
    def __init__(self,name,age):
        self.name =name
        self.age =age
obj = Human("账号",23)
print(obj.__dict__)#{'name': '账号', 'age': 23}
print(obj)#<__main__.Human object at 0x00000274F886BFD0>
obj1 = Human("账号",25)
print(obj.__dict__)#{'name': '账号', 'age': 25}
print(obj1)#<__main__.Human object at 0x00000274F886BFD0>
# 最后提一下 , None 在python里面就是一个单例模式
# 这就是为什么我们用 None组判断是 if is None
# 因为None只有一个对象,只要某一个对象的变量值是None,那他一定是同一个对象
    人生应该删繁留简,任世事摇曳,心始终如莲,安静绽放。 --白落梅
                    
                
                
            
        
浙公网安备 33010602011771号