** Singleton ** ** 设计模式的 ** ** C# ** ** 实现 ** ** **
** 电子科技大学 ** ** ** ** 张申 ** ** ** ** ( [email protected] ) **
关键字: singleton 设计模式 同步 C#
** 1 ** ** Singleton ** ** 模式。 ** ** **
Singleton (译为单件或单态)模式是设计模式中比较简单而常用的模式。
有些时候在整个应用程序中,会要求某个类有且只有一个实例,这个时候可以采用 Singleton 模式进行设计。用 Singleton 模式设计的类不仅能保证在应用中只有一个实例,而且提供了一种非全局变量的方法进行全局访问,称为全局访问点,这样对于没有全局变量概念的纯面向对象语言来说是非常方便的,比如 C# 。
本文用一个计数器的例子来描述在 C# 中如何使用 Singleton 模式:计数的值设计为计数器类的一个私有成员变量,它被 4 个不同的线程进行读写操作,为保证计数的正确性,在整个应用当中必然要求计数器类的实例是唯一的。
** 2 ** ** Singleton ** ** 的实现方式。 ** ** **
首先看看教科书方式的 Singleton 标准实现的两种方法,以下用的是类 C# 伪代码:
方法一:
using System;
namespace csPattern.Singleton
{
public class Singleton
{
static Singleton uniSingleton = new Singleton();
private Singleton() {}
static public Singleton instance()
{
return uniSingleton;
}
}
}
方法二:
using System;
namespace csPattern.Singleton
{
public class Singleton
{
static Singleton uniSingleton;
private Singleton() {}
static public Singleton instance()
{
if (null == uniSingleton)
{
uniSingleton = new Singleton _lazy();
}
return uniSingleton;
}
}
}
Singleton 模式的实现有两个技巧:一是使用静态成员变量保存“全局”的实例,确保了唯一性,使用静态的成员方法 instance() 代替 new 关键字来获取该类的实例,达到全局可见的效果。二是将构造方法设置成为 private ,如果使用 new 关键字创建类的实例,则编译报错,以防编程时候笔误。
上面方法二的初始化方式称为 lazy initialization ,是在第一次需要实例的时候才创建类的实例,与方法一中类的实例不管用不用一直都有相比,方法二更加节省系统资源。但是方法二在多线程应用中有时会出现多个实例化的现象。
假设这里有 2 个线程:主线程和线程 1 ,在创建类的实例的时候可能会遇到一些原因阻塞一段时间(比如网络速度或者需要等待某些正在使用的资源的释放),此时的运行情况如下:
主线程首先去调用 instance() 试图获得类的实例, instance() 成员方法判断该类没有创建唯一实例,于是开始创建实例。由于一些因素,主线程不能马上创建成功,而需要等待一些时间。此时线程 1 也去调用 instance() 试图获得该类的实例,因为此时实例还未被主线程成功创建,因此线程 1 又开始创建新实例。结果是两个线程分别创建了两次实例,对于计数器类来说,就会导致计数的值被重置,与 Singleton 的初衷违背。解决这个问题的办法是同步。
下面看看本文的计数器的例子的实现:
使用方法一:
using System;
using System.Threading;
namespace csPattern.Singleton
{
public class Counter
{
static Counter uniCounter = new Counter(); // 存储唯一的实例。
private int totNum = 0; // 存储计数值。
private Counter()
{
Thread.Sleep(100); // 这里假设因为某种因素而耽搁了 100 毫秒。
// 在非 lazy initialization 的情况下 , 不会影响到计数。 .
}
static public Counter instance()
{
return uniCounter;
}
public void Inc() { totNum ++;} // 计数加 1 。
public int GetCounter() { return totNum;} // 获得当前计数值。
}
}
以下是调用 Counter 类的客户程序,在这里我们定义了四个线程同时使用计数器,每个线程使用 4 次,最后得到的正确结果应该是 16 :
using System;
using System.IO;
using System.Threading;
namespace csPattern.Singleton.MutileThread
{
public class MutileClient
{
public MutileClient() {}
public void DoSomeWork()
{
Counter myCounter = Counter.instance(); // 方法一
//Counter_lazy myCounter = Counter_lazy.instance(); // 方法二
for (int i = 1; i < 5; i++)
{
myCounter.Inc();
Console.WriteLine(" 线程 {0} 报告 : 当前 counter 为 : {1}", Thread.CurrentThread.Name.ToString(), myCounter.GetCounter().ToString());
}
}
public void ClientMain()
{
Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 =new Thread(new ThreadStart(this.DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 =new Thread(new ThreadStart(this.DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 =new Thread(new ThreadStart(this.DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
DoSomeWork(); // 线程 0 也只执行和其他线程相同的工作。
}
}
}
(接下半部分)