设计模式——单例模式

单例模式

单例模式确保程序运行过程中一个特定的类只有一个实例对象,并提供对这个对象的外部访问
常见的单例模式应用如线程池、缓存、日志对象等
特点:
单例类必须自己创建自己的唯一实例,并且给其它对象提供这一个实例
应用场景:
1.工具类
2.需要重复创建/销毁的资源消耗过大的类
3.等等

饿汉式单例:程序一启动就创建对象实例,占用内存资源,提高程序效率(典型的以空间换时间)
懒汉式单例:程序一启动不创建对象实例,什么时候用到对象实例什么时候创建,节省资源

Java实现

//饿汉式——避免的线程同步问题,但是如果没有调用就会占用资源,没有达到懒加载的效果
public class Singleton {
	//静态成员属性——类加载时完成实例化,避免线程安全的问题
	//没有达到懒加载的效果
	//方式一
    private final static Singleton SINGLE = new Singleton();

	/*方式二 静态代码块——优缺点与静态成员一致
	private final static Singleton SINGLE ;
	static {
        instance = new Singleton();
    }
	*/
    public static Singleton getInstance(){
        return SINGLE ;
    }
}
//懒汉式——达到了懒加载的效果,但是需要解决线程同步问题
public class Singleton {

    private static volatile Singleton single;

	//使用双重检查来保证线程的安全
    public static Singleton getInstance() {
        if (single== null) {
            synchronized (Singleton.class) {
                if (single== null) {
                    single= new Singleton();
                }
            }
        }
        return single;
    }
}
//使用静态内部类的方式获取单例
//利用了类加载机制来保证实例化时只有一个线程
//静态内部类方式在Singleton类被装载时并不会立即实例化
//而是在调用getSingleton()方法时实例化,达到了懒加载的效果
public class Singleton {

    private static class SingletonInstance {
        private static final Singleton SINGLE = new Singleton();
    }

    public static Singleton getSingleton() {
        return SingletonInstance.SINGLE;
    }
}

Python实现

__new__方法实现的单例

__new__实例化方法
__init__初始化方法
类()实例化时,会先去自身的new方法,没有就去object的new方法来创建对象,再返回给self
此时self是不存在的,所以用cls表示这个类,用cls来实例化对象,再赋值给self
然后运行init方法给self初始化(给对象设置属性)
class Singleton(object):
    __instance = None

    def __init__(self,name,age):
        self.name = name
        self.age = age 
    
    def __new__(cls,*args,**kwargs):
        if not cls.__instance:
            cls.__instance = object.__new__(cls)
        return cls.__instance
'''
	new方法的方式二:利用反射完成单例
	def __new__(cls,*args,**kwargs):
        if not hasattr(cls,'__instance'): # 如果当前类中没有__instance全局变量就创建一个,变量的值为当前类的实例
            cls.__instance= super(Singleton,cls).__new__(cls)
    # 等价于 cls.__instance = object.__new__(cls)
        return cls.__instance # 返回这个变量
	
'''
    
s1 = Singleton('张三','23')
print(id(s1))   #32079112
s2 = Singleton('李四','24')
print(id(s2))   #32079112

'''
当Singleton('张三','23')对象实例化时,调用__new__方法进行对象的实例化,
类中的变量__instance 为空,所以去创建对象实例用于赋值,然后返回这个实例对象
然后调用__init__为属性赋值

当Singleton('李四','24')对象实例化时,调用__new__方法进行对象的实例化,
类中的变量__instance为全局变量,所以对象共享,此时__instance有值,不走if体中的语句,直接返回__instance的值(上一个实例化对象)
然后调用__init__为属性赋值(覆盖掉上一个对象的属性赋值)
'''

使用装饰器的单例

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 Student(object):
    def __init__(self, name):
        self.name = name


a1 = Student('zs')
print(id(a1))	#40288520
a2 = Student('ls')
print(id(a1))	#40288520

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值