单例模式介绍

【一】为什么要单例模式

单例设计模式:

  • 一个类只允许创建一个对象(或者实例),那这个类就是一个单例类,这种设计模式就叫作单例设计模式,简称单例模式。

  • 当一个类的功能比较单一,只需要一个实例对象就可以完成需求时,就可以使用单例模式来节省内存资源。

【二】如何实现一个单例

  • 要实现一个单例,我们需要知道要重点关注的点是哪些?

    • 考虑对象创建时的线程安全问题

    • 考虑是否支持延迟加载

    • 考虑获取实例的性能是否高(是否加锁)

  • 在python中,我们可以使用多种方法来实现单例模式

    • 使用模块

    • 使用装饰器

    • 使用类(方法)

    • 基于__new__方法实现

    • 基于元类metaclass实现

# 创建一个普通的类
class Student:
    ...
​
​
# 实例化类得到对象 --- 类只要加 () 实例化就会产生一个全新的对象
# 查看对象  --- 发现虽然是同一个类实例化得到的对象,但是对象的地址不一样
# 每次使用都要重新打开,内存占用过高
stu_one = Student()
print(id(stu_one))  # 1764834580640
stu_two = Student()
print(id(stu_two))  # 1764834304080

单例模式的实现

# 方式一:
class MyType(type):
    def __init__(cls,class_name,class_bases,class_name_space):
        # 给自己的类中加一个属性 instance 初始值是none
        cls.instance = None
        super().__init__(class_name,class_bases,class_name_space)
​
    def __call__(self, *args, **kwargs):
        obj = super().__call__(*args,**kwargs)
        # 判断 只要有就返回他不创建,没有就创建再返回
        if not self.instance:
            self.instance =obj
        return self.instance
​
​
class Student(metaclass=MyType):
    ...
​
​
stu_one = Student()
print(id(stu_one))      # 2645492891216
stu_two = Student()
print(id(stu_two))      # 2645492891216
​
# 定制类的属性 ---》 元类 __init__
# 定制对象的属性 ---> 元类 __call__
​
​
# 方式二
# 使用装饰器
# outer 接受cls类作为参数
def outer(cls):
    # 定义一个字典,键为cls,初始值为none,永远存储cls的单例实例
    instance = {'cls':None}
​
    def inner(*args, **kwargs):
        # 使用nonlocal关键字声明 instance变量,这意味着inner函数将修改在outer函数中定义的instance变量,而不是创建一个新的
        nonlocal instance
        # 进行判断是否有cls
        if not instance['cls']:
            instance['cls'] = cls(*args, **kwargs)
        return instance['cls']
​
    return inner
​
​
@outer
class Student:
    ...
​
​
stu_one = Student()
print(id(stu_one))      # 1384468577632
stu_two = Student()
print(id(stu_two))      # 1384468577632
  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值