设计模式--01单例模式

单例模式

优点

  • 由于内存中只有一个实例,减少了内存开支,减少了系统性能开销
  • 避免对资源的多重占用,例如写文件动作
  • 设置全局变量,优化和共享资源访问。例如,可以设计一个单例类,负责所有数据的映射处理

缺点

  • 单例模式一般没有接口,扩展困难,只有修改代码。单例模式为什么不增加借口呢?因为接口对单例模式是没有任何意义的,他要求‘’‘自行实例化’,并且提供单一实例,接口或抽象类是不能被实例化的。在特定情况下是可以实现接口、被继承,视情况而定
  • 对测试不利,没有接口不能使用mock的方法虚拟一个对象
  • 单例模式和单一职责原则有冲突。一个类应该只实现一个逻辑。而不关心是否单例

使用场景

在一个系统,要求一个类有且只有一个对象,可采用单例模式。

  • 要求生成唯一序列号的环境
  • 在整个项目上需要一个共享访问点或共享数据,例如:计数器
  • 创建一个对象需要消耗过多资源,如要访问IO和数据库等资源
  • 需要定义大量的静态常量和静态方法(也可使用static)

注意事项

在高并发情况下,注意单例模式的线程同步问题,进程加锁

扩展

使用单例模式产生固定数量对象

Python实现

单例模式的实现过程

使用模块

Python的模块是天然的单例模式

class Singleton(object):
    def foo(self):
        pass
singleton = Singleton()

使用装饰器

def Singleton(cls):
    _instance = {}
    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]
    return _singleton

@Singleton
class A(object):
    a = 1
    def __init__(self, x=0):
        self.x = x

使用类

import time
import threading
class Singleton(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        time.sleep(1)

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = Singleton(*args, **kwargs)
        return Singleton._instance

def task(arg):
    obj = Singleton.instance()
    print(obj)
for i in range(10):
    t = threading.Thread(target=task,args=[i,])
    t.start()
time.sleep(20)
obj = Singleton.instance()
print(obj)

基于__new__方法实现(推荐)

实现单例时,为了保证线程安全需要在内部加锁,实例化对象时,先执行了__new__方法,然后再执行__init__方法

import threading
class Singleton(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        pass


    def __new__(cls, *args, **kwargs):
        if not hasattr(Singleton, "_instance"):
            with Singleton._instance_lock:
                if not hasattr(Singleton, "_instance"):
                    Singleton._instance = object.__new__(cls)  
        return Singleton._instance

obj1 = Singleton()
obj2 = Singleton()
print(obj1,obj2)

def task(arg):
    obj = Singleton()
    print(obj)

for i in range(10):
    t = threading.Thread(target=task,args=[i,])
    t.start()

采用这种方式的单例模式,需要实例化对象,obj = Singleton()

基于metaclass方式实现

  • 类由type创建,创建类时,type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法
  • 对象由类创建,创建对象时,类的__init__方法自动执行,对象()执行类的 __call__ 方法
import threading

class SingletonType(type):
    _instance_lock = threading.Lock()
    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instance_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)
        return cls._instance

class Foo(metaclass=SingletonType):
    def __init__(self,name):
        self.name = name


obj1 = Foo('name')
obj2 = Foo('name')
print(obj1,obj2)

Java实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值