单例模式的应用:
C# 基础(十四)C#单例模式:首先介绍 单线程、多线程、加锁 单例模式。然后介绍单例模式的线程同步:多线程有序访问共享内存。
二、单例模式的应用 1、好了,以上问题虽然讨论的单例模式的相关理论,但是却么有怎么应用。现在,我把单例模式应用起来。建立C#控制台程序,我用到了三个类:
2、不多说了,现在直接把程序附上,自己慢慢调试,可以注释掉方法2/3,剩下方法1,看看效果。
也可以全部保留全部的方法,然后单部执行,你会发现只有方法1进行了实例化,方法2/3就不会进行实例化了。
Program.cs
#region 单实例类 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Messaging; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml.Linq; namespace test { class Program { static void Main(string[] args) { //方法1:中间类ClassB调用了单实例类Singleton,通过实例化中间类ClassB。 ClassB classb = new ClassB(); classb.methodB(); //方法2:直接实例化Singleton。 Singleton singleton = Singleton.GetInstance(); Console.WriteLine(singleton.iGlobalNum1); //方法3:调用方法GetSingleton()来实例化Singleton。 GetSingleton(); Console.ReadLine(); } static private void GetSingleton() { Singleton singleton = Singleton.GetInstance(); Console.WriteLine(singleton.bGlobalStatus1); } } } #endregion ClassB.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using test; namespace test { class ClassB { Singleton singleton = Singleton.GetInstance(); public void methodB() { Console.WriteLine(singleton.iGlobalNum1); //iGlobalNum2字段,是私有的,故不能被访问。 //Console.WriteLine(singleton.iGlobalNum2); Console.WriteLine(singleton.bGlobalStatus1); } } } Singleton.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test { public class Singleton { public int iGlobalNum1; private int iGlobalNum2; public bool bGlobalStatus1; // 定义一个静态变量来保存类的实例 private volatile static Singleton uniqueInstance; // 定义私有构造函数,使外界不能创建该类实例 private Singleton() { iGlobalNum1 = 11; iGlobalNum2 = 22; bGlobalStatus1 = false; } /// /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// /// public static Singleton GetInstance() { // 如果类的实例不存在则创建,否则直接返回 if (uniqueInstance == null) { uniqueInstance = new Singleton(); } return uniqueInstance; } } }
适用场景:
单例模式(Singleton)也叫单态模式,是设计模式中最为简单的一种模式,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象,也因此有些设计大师把其称为设计模式之一。
1. Windows的Task Manager(“任务管理器”,不是“文件资源管理器”)是很典型的单例模式,还有自带的windows media player播放器, 想想看,能同时打开两个吗?
2. windows的Recycle Bin(回收站)也是典型的单例应用。在整个系统运行过程中,回收站一直维护着仅有的一个实例。
3. 网站的计数器,一般也是采用单例模式实现,否则难以同步。
4. 应用程序的日志应用,一般都何用单例模式实现,这一般是由于共享的日志文件一直处于打开状态,因为只能有一个实例去操作,否则内容不好追加。
5. Web应用的配置对象的读取,一般也应用单例模式,这个是由于配置文件是共享的资源。
6. 数据库连接池的设计一般也是采用单例模式,因为数据库连接是一种数据库资源。数据库软件系统中使用数据库连接池,主要是节省打开或者关闭数据库连接所引起的效率损耗,这种效率上的损耗还是非常昂贵的,因为何用单例模式来维护,就可以大大降低这种损耗。
7. 多线程的线程池的设计一般也是采用单例模式,这是由于线程池要方便对池中的线程进行控制。
8. 操作系统的文件系统,也是大的单例模式实现的具体例子,一个操作系统只能有一个文件系统。
9. HttpApplication 也是单位例的典型应用。熟悉ASP.Net(IIS)的整个请求生命周期的人应该知道HttpApplication也是单例模式,所有的HttpModule都共享一个HttpApplication实例.
10. 游戏中的“场景管理器”,用来管理游戏场景的切换、资源载入、网络连接等等任务。这个管理器需要有多种方法和属性,在代码中很多地方会被调用,且被调用的必须是同一个管理器,否则既容易产生冲突,也会浪费资源。
总结以上,不难看出:
单例模式应用的场景一般发现在以下条件下:
(1)资源共享的情况下,避免由于资源操作时导致的性能或损耗等。如上述中的日志文件,应用配置。
(2)控制资源的情况下,方便资源之间的互相通信。如线程池等。
单例模式就是确保一个类只有一个实例.当你希望整个系统中,某个类只有一个实例时,单例模式就派上了用场. 比如,某个服务器的配置信息存在在一个文件中,客户端通过AppConfig类来读取配置文件的信息.如果程序的运行的过程中,很多地方都会用到配置文件信息,则就需要创建很多的AppConfig实例,这样就导致内存中有很多AppConfig对象的实例,造成资源的浪费.其实这个时候AppConfig我们希望它只有一份,就可以使用单例模式.
单例模式是日常应用中最广泛的模式了,其目的就是令到单个进程中只存在一个类的实例,从而可以实现数据的共享,节省系统开销,防止io阻塞等等.但是在多进程的应用中,单例模式就实现不了了,例如一些web应用,django,这些,因为会启动多条进程来监听http请求,这样的会通过单例模式是实现不了数据共享的,也就是实现不了单例模式的目的了,这时需要用进程间通信方法来实现数据共享,当然也可以尝试使用redis这些nosql数据库实现数据共享,因为它们的读取数据较快。
吉森又想到了一个问题,继续挑战特使:“Python连个private 关键字都没有,怎么隐藏一个类的构造函数,怎么去实现单例?”
特使不屑地说:“忘掉你那套Java思维吧,在Python中想写个singleton有很多办法,我给你展示一个比较Python的方式,用module的方式来实现。”
-
#singleton.py
-
class Singleton:
-
def __init__(self):
-
self.name = "i'm singleton"
-
instance = Singleton()
-
del Singleton # 把构造函数删除
-
import singleton
-
print(singleton.instance.name) # i'm singleton
-
instance = Singleton() # NameError: name 'Singleton' is not defined
吉森确实没有想到这种写法,利用Python的module来实现信息的隐藏。
实现单例模式的几种方法
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.单例类
// 单例模式-饿汉式 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.单例类
单例模式-懒汉式 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.单例类
单例模式-懒汉式 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.测试结果
原理:使用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 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 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 map = instance1.getMap();
for (Map.Entry item : map.entrySet()) {
System.out.println("map-item:" + item.getKey() + "=" + item.getValue());
}
System.out.println("instance1和instance2是否为同一实例?" + (instance1 == instance2));
System.out.println("-----------------测试登记式单例模式结束--------------");
}
}
5.测试结果
该解决方案的缺点:基类的构造函数对子类公开了(protected),有好的解决方案的博友可以讨论指教~
八、总结经过本文,我们就搞明白了什么叫单例模式,如何优雅的实现经典的单例模式,如何进行拓展和开发具有线程安全的单例模式