单例模式
单例模式就是确保一个类只有一个实例.当你希望整个系统中,某个类只有一个实例时,单例模式就派上了用场.
比如,某个服务器的配置信息存在在一个文件中,客户端通过AppConfig类来读取配置文件的信息.如果程序的运行的过程中,很多地方都会用到配置文件信息,则就需要创建很多的AppConfig实例,这样就导致内存中有很多AppConfig对象的实例,造成资源的浪费.其实这个时候AppConfig我们希望它只有一份,就可以使用单例模式.
实现单例模式的几种方法
1. 使用模块
其实,python的模块就是天然的单例模式,因为模块在第一次导入的时候,会生成.pyc文件,当第二次导入的时候,就会直接加载.pyc文件,而不是再次执行模块代码.如果我们把相关的函数和数据定义在一个模块中,就可以获得一个单例对象了.
新建一个python模块叫singleton,然后常见以下python文件mysingleton.py
class Singleton(object):
def foo(self):
pass
singleton = Singleton()
使用:
from singleton.mysingleton import singleton
2. 使用装饰器
装饰器里面的外层变量定义一个字典,里面存放这个类的实例.当第一次创建的收,就将这个实例保存到这个字典中.
然后以后每次创建对象的时候,都去这个字典中判断一下,如果已经被实例化,就直接取这个实例对象.如果不存在就保存到字典中.
# encoding:utf-8
__author__ = 'Fioman'
__time__ = '2019/3/6 10:22'
def singleton(cls):
# 单下划线的作用是这个变量只能在当前模块里访问,仅仅是一种提示作用
# 创建一个字典用来保存类的实例对象
_instance = {}
def _singleton(*args, **kwargs):
# 先判断这个类有没有对象
if cls not in _instance:
_instance[cls] = cls(*args, **kwargs) # 创建一个对象,并保存到字典当中
# 将实例对象返回
return _instance[cls]
return _singleton
@singleton
class A(object):
a = 1
def __init__(self, x=0):
self.x = x
print('这是A的类的初始化方法')
a1 = A(2)
a2 = A(3)
print(id(a1), id(a2))
3.使用类
思路就是,调用类的instance方法,这样有一个弊端就是在使用类创建的时候,并不是单例了.也就是说在创建类的时候一定要用类里面规定的方法创建
# encoding:utf-8
__author__ = 'Fioman'
__time__ = '2019/3/6 11:06'
class Singleton(object):
def __init__(self,*args,**kwargs):
pass
@classmethod
def get_instance(cls, *args, **kwargs):
# 利用反射,看看这个类有没有_instance属性
if not hasattr(Singleton, '_instance'):
Singleton._instance = Singleton(*args, **kwargs)
return Singleton._instance
s1 = Singleton() # 使用这种方式创建实例的时候,并不能保证单例
s2 = Singleton.get_instance() # 只有使用这种方式创建的时候才可以实现单例
s3 = Singleton()
s4 = Singleton.get_instance()
print(id(s1), id(s2), id(s3), id(s4))
注意,这样的单例模式在单线程下是安全的,但是如果遇到多线程,就会出现问题.如果遇到多个线程同时创建这个类的实例的时候就会出现问题.
# encoding:utf-8
__author__ = 'Fioman'
__time__ = '2019/3/6 11:26'
import threading
class Singleton(object):
def __init__(self, *args, **kwargs):
pass
@classmethod
def get_instance(cls, *args, **kwargs):
if not hasattr(Singleton, '_instance'):
Singleton._instance = Singleton(*args, **kwargs)
return Singleton._instance
def task(arg):
obj = Singleton.get_instance(arg)
print(obj)
for i in range(10):
t = threading.Thread(target=task, args=[i, ])
t.start()
执行结果好像也没有问题,那是因为执行的速度足够的快,如果在init()方法中有阻塞,就看到非常的明显.
# encoding:utf-8
__author__ = 'Fioman'
__time__ = '2019/3/6 11:26'
import threading
import time
class Singleton(object):
def __init__(self, *args, **kwargs):
time.sleep(1)
pass
@classmethod
def get_instance(cls, *args, **kwargs):
if not hasattr(Singleton, '_instance'):
Singleton._instance = Singleton(*args, **kwargs)
return Singleton._instance
def task(arg):
obj = Singleton.get_instance(arg)
print(obj)
for i in range(10):
t = threading.Thread(target=task, args=[i, ])
t.start()
可以看到是创建了10个不同的实例对象,这是什么原因呢.因为在一个对象创建的过程中,另外一个对象也创建了.当它判断的时候,会先去获取_instance属性,因为这个时候还没有,它就会调用init()方法.结果就是调用了10次,然后就创建了10个对象.
如何解决呢?加锁:
在哪里加锁呢?在获取对象属性_instance的时候加锁,如果已经有人在获取对象了,其他的人如果要获取这个对象,就要等一哈.因为前面的那个人,可能在第一次创建对象.
创建对象的时候加锁即可
# encoding:utf-8
__author__ = 'Fioman'
__time__ = '2019/3/6 11:38'
import time
import threading
class Singleton(object):
_instance_lock = threading.Lock()
def __init__(self,*args,**kwargs):
time.sleep(1)
@classmethod
def get_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.get_instance(arg)
print(obj)
for i in range(10):
t = threading.Thread(target=task,args=[i,])
t.start()
obj = Singleton.get_instance()
print(obj)
这种方式创建的单例,必须使用Singleton_get_instance()方法,如果使用Singleton()的话,得到的并不是单例.所以我们推荐使用__new__()方法来创建单例,这样创建的单例可以使用类名()的方法进行实例化对象
4.基于__new__
方法实现的单例模式(推荐使用,方便)
知识点:
1> 一个对象的实例化过程是先执行类的__new__方法
,如果我们没有写,默认会调用object的__new__
方法,返回一个实例化对象,然后再调用__init__方法
,对这个对象进行初始化,我们可以根据这个实现单例.
2> 在一个类的__new__方法中
先判断是不是存在实例,如果存在实例,就直接返回,如果不存在实例就创建.
# encoding:utf-8
__author__ = 'Fioman'
__time__ = '2019/3/6 13:36'
import threading
class Singleton(object):
_instance_lock = threading.Lock()
def __init__(self, *args, **kwargs):
pass
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
with Singleton._instance_lock:
if not hasattr(cls, '_instance'):
Singleton._instance = super().__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()
其它语言的单例实现
1.为什么要使用单例模式?
在我们日常的工作中,很多对象通常占用非常重要的系统资源,比如:IO处理,数据库操作等,那我们必须要限制这些对象只有且始终使用一个公用的实例,即单例。
2.单例模式的实现方式
- 构造函数私有化,防止其他类生成唯一公用实例外的实例。且
- 单例类应该被定义为final,也就是说单例类不能被继承,因为如果允许继承那子类就都可以创建实例,违背了类唯一实例的初衷。
- 类中一个静态变量来保存单实例的引用。
- 一个共有的静态方法来获取单实例的引用。
3.单例模式的UML类图
4.单例模式的经典实现方式
- 饿汉式:一开始就创建好实例,每次调用直接返回,经典的“拿空间换时间”。
- 懒汉式:延迟加载,第一次调用的时候才加载,然后返回,以后的每次的调用就直接返回。经典“拿时间换空间”,多线程环境下要注意解决线程安全的问题。
- 登记式:对一组单例模式进行的维护,主要是在数量上的扩展,通过线程安全的map把单例存进去,这样在调用时,先判断该单例是否已经创建,是的话直接返回,不是的话创建一个登记到map中,再返回。
三、饿汉式---代码实现
1.单例类
package com.hafiz.designPattern.singleton; /** * Desc: 单例模式-饿汉式 * Created by hafiz.zhang on 2017/9/26. */ public class Singleton1 { // 创建全局静态变量,保证只有一个实例 private static volatile Singleton1 instance = new Singleton1(); private Singleton1() { // 构造函数私有化 System.out.println("--调用饿汉式单例模式的构造函数--"); } public static Singleton1 getInstance() { System.out.println("--调用饿汉式单例模式的静态方法返回实例--"); return instance; } }
2.测试类
public class DesignPatternTest { @Test public void testSingleton1() { System.out.println("-----------------测试饿汉式单例模式开始--------------"); Singleton1 instance1 = Singleton1.getInstance(); System.out.println("第二次获取实例"); Singleton1 instance2 = Singleton1.getInstance(); System.out.println("instance1和instance2是否为同一实例?" + (instance1 == instance2)); System.out.println("-----------------测试饿汉式单例模式结束--------------"); } }
3.测试结果
四、懒汉式---代码实现
1.单例类
package com.hafiz.designPattern.singleton; /** * Desc:单例模式-懒汉式 * Created by hafiz.zhang on 2017/9/26. */ public class Singleton2 { // 创建全局静态变量,保证只有一个实例 private static Singleton2 instance = null; // 构造函数私有化 private Singleton2() { System.out.println("--调用懒汉式单例模式的构造方法--"); } public static Singleton2 getInstance() { System.out.println("--调用懒汉式单例模式获取实例--"); if (instance == null) { System.out.println("--懒汉式单例实例未创建,先创建再返回--"); instance = new Singleton2(); } return instance; } }
2.测试类
public class DesignPatternTest { @Test public void testSingleton2() { System.out.println("-----------------测试懒汉式单例模式开始--------------"); Singleton2 instance1 = Singleton2.getInstance(); System.out.println("第二次获取实例"); Singleton2 instance2 = Singleton2.getInstance(); System.out.println("instance1和instance2是否为同一实例?" + (instance1 == instance2)); System.out.println("-----------------测试懒汉式单例模式结束--------------"); } }
3.测试结果
细心的同学已经发现,这种实现方式,在多线程的环境中,是有线程安全安全问题的,有可能两个或多个线程判断instance都为null,然后创建了好几遍实例,不符合单例的思想,我们可以对它进行改进。
五、改进懒汉式1---代码实现
原理:使用JDK的synchronized同步代码块来解决懒汉式线程安全问题。
1.单例类
package com.hafiz.designPattern.singleton; /** * Desc:单例模式-懒汉式 * Created by hafiz.zhang on 2017/9/26. */ public class Singleton2 { // 创建全局静态变量,保证只有一个实例 private static Singleton2 instance = null; // 构造函数私有化 private Singleton2() { System.out.println("--调用懒汉式单例模式的构造方法--"); } public static Singleton2 getInstance() { System.out.println("--调用懒汉式单例模式获取实例--"); if (instance != null) { System.out.println("--懒汉式单例实例已经创建,直接返回--"); return instance; } synchronized (Singleton2.class) { if (instance == null) { System.out.println("--懒汉式单例实例未创建,先创建再返回--"); instance = new Singleton2(); } } return instance; } }
2.测试结果
六、改进懒汉式2---代码实现
原理:使用JVM隐含的同步和类级内部类来解决,JVM隐含的同步解决了多线程情况下线程安全的问题,类级内部类解决只有使用的时候才加载(延迟加载)的问题。
1.JVM隐含的同步有哪些?
- 静态初始化器(在静态字段上或static{}静态代码块的初始化器)初始化数据时
- 访问final字段时
- 在创建线程之前创建对象时
- 线程可以看见它将要处理的对象时
2.什么是类级内部类?
- 有static修饰的成员式内部类。没有static修饰的成员式内部类叫对象级内部类。
- 类级内部类相当于其外部类的static成分,他的对象与外部类对象间不存在依赖关系,因此可直接创建,而对象级内部类的实例,是绑定在外部对象实例中的。
- 类级内部类中,可以定义静态的方法。在静态的方法中只能够引用外部类的中的静态成员方法或者成员变量
- 类级内部类相当于其外部类的成员,只有在第一次被使用的时候才会被装载
3.单例类
package com.hafiz.designPattern.singleton; /** * Desc:单例模式-改进懒汉式 * Created by hafiz.zhang on 2017/9/26. */ public class Singleton3 { private static class Singleton4 { private static Singleton3 instance; static { System.out.println("--类级内部类被加载--"); instance = new Singleton3(); } private Singleton4() { System.out.println("--调用类级内部类的构造函数--"); } } private Singleton3() { System.out.println("--调用构造函数--"); } public static Singleton3 getInstance() { System.out.println("--开始调用共有方法返回实例--"); Singleton3 instance; System.out.println("---------------------------"); instance = Singleton4.instance; System.out.println("返回单例"); return instance; } }
4.测试类
package com.hafiz.www; import com.hafiz.designPattern.observer.ConcreteObserver; import com.hafiz.designPattern.observer.ConcreteSubject; import com.hafiz.designPattern.singleton.Singleton1; import com.hafiz.designPattern.singleton.Singleton2; import com.hafiz.designPattern.singleton.Singleton3; import com.hafiz.designPattern.singleton.Singleton4; import com.hafiz.designPattern.singleton.Singleton4Child1; import com.hafiz.designPattern.singleton.SingletonChild2; import org.junit.Test; /** * Desc:设计模式demo单元测试类 * Created by hafiz.zhang on 2017/7/27. */ public class DesignPatternTest { @Test public void testSingleton3() { System.out.println("-----------------测试改进懒汉式单例模式开始--------------"); Singleton3 instance1 = Singleton3.getInstance(); System.out.println("第二次获取实例"); Singleton3 instance2 = Singleton3.getInstance(); System.out.println("instance1和instance2是否为同一实例?" + (instance1 == instance2)); System.out.println("-----------------测试改进懒汉式单例模式结束--------------"); } }
5.测试结果
七、登记式--代码实现
1.基类
package com.hafiz.designPattern.singleton; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Desc: 单例模式-登记式 * Created by hafiz.zhang on 2017/9/26. */ public class Singleton4 { private static Map<String, Singleton4> map = new ConcurrentHashMap<>(); protected Singleton4() { System.out.println("--私有化构造函数被调用--"); } public static Singleton4 getInstance(String name) { if (name == null) { name = Singleton4.class.getName(); System.out.println("--name为空,默认赋值为:--" + Singleton4.class.getName()); } if (map.get(name) != null) { System.out.println("name对应的值存在,直接返回"); return map.get(name); } System.out.println("name对应的值不存在,先创建,再返回"); try { Singleton4 result = (Singleton4)Class.forName(name).newInstance(); map.put(name, result); return result; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } public Map<String, Singleton4> getMap() { return map; } }
2.子类1
package com.hafiz.designPattern.singleton; /** * Desc: * Created by hafiz.zhang on 2017/9/26. */ public class Singleton4Child1 extends Singleton4 { public static Singleton4Child1 getInstance() { return (Singleton4Child1) Singleton4.getInstance("com.hafiz.designPattern.singleton.Singleton4Child1"); } }
3.子类2
package com.hafiz.designPattern.singleton; /** * Desc: * Created by hafiz.zhang on 2017/9/26. */ public class SingletonChild2 extends Singleton4 { public static SingletonChild2 getInstance() { return (SingletonChild2) Singleton4.getInstance("com.hafiz.designPattern.singleton.SingletonChild2"); } }
4.测试类
public class DesignPatternTest { @Test public void testSingleton4() { System.out.println("-----------------测试登记式单例模式开始--------------"); System.out.println("第一次取得实例"); Singleton4 instance1 = Singleton4.getInstance(null); System.out.println("res:" + instance1); System.out.println("第二次获取实例"); Singleton4Child1 instance2 = Singleton4Child1.getInstance(); System.out.println("res:" + instance2); System.out.println("第三次获取实例"); SingletonChild2 instance3 = SingletonChild2.getInstance(); System.out.println("res:" + instance3); System.out.println("第四次获取实例"); SingletonChild2 instance4 = new SingletonChild2(); System.out.println("res:" + instance4); System.out.println("输出父类Map中所有的单例"); Map<String, Singleton4> map = instance1.getMap(); for (Map.Entry<String, Singleton4> item : map.entrySet()) { System.out.println("map-item:" + item.getKey() + "=" + item.getValue()); } System.out.println("instance1和instance2是否为同一实例?" + (instance1 == instance2)); System.out.println("-----------------测试登记式单例模式结束--------------"); } }
5.测试结果
该解决方案的缺点:基类的构造函数对子类公开了(protected),有好的解决方案的博友可以讨论指教~
八、总结
经过本文,我们就搞明白了什么叫单例模式,如何优雅的实现经典的单例模式,如何进行拓展和开发具有线程安全的单例模式
来源:CSDN
作者:peter-jh
链接:https://blog.csdn.net/qq_42672770/article/details/103998715