参考的文档
http://www.cnblogs.com/zhenyulu/articles/37246.html
一 什么是单例模式
单例要求一个类里面只有一个实例,并且提供了一个全局的访问点。
单例类里面定义了一个GetInstance的方法,是一个静态方法。用于创建自己的唯一实例。
二 什么情况使用单例模式
在一个系统里面要求某个类只有一个实例才能使用单例模式。
比如打印机 或者一个通讯口进行传输。
三 一个简单的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Singleton
{
// "Singleton"
class Singleton
{
private static Singleton instance;
// Constructor
protected Singleton()
{
}
// Methods
public static Singleton GetInstance()
{
// Uses "Lazy initialization"
if (instance == null)
instance = new Singleton();
return instance;
}
}
public class Client
{
public static void Main()
{
// Constructor is protected -- cannot use new
Singleton s1 = Singleton.GetInstance();
Singleton s2 = Singleton.GetInstance();
if (s1 == s2)
Console.WriteLine("The same instance");
}
}
}
看程序
1 静态的成员变量。
2 私有或者受保护的构造函数,不允许通过new的方式创建实例。
3 通过开放GetInstance方法来获取实例。可以保证只初始化一次。
四 实现的几种方法
1. 针对多线程的情况,需要加入lock。
其中双重锁定比较合适,不用每次都lock影响效率。
如果Instanch不为空,不续约进入锁状态。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Singleton
{
public sealed class Singleton
{
static Singleton instance = null;
static readonly object lock1 = new object();
Singleton()
{
}
public static Singleton GetInstance()
{
if (instance == null)
{
lock (lock1)
{
if (instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}
}
}
sealed关键字声明此类不能被继承。
2. 延迟初始化
public sealed class Singleton
{
Singleton()
{
}
public static Singleton GetInstance()
{
return Nested.instance;
}
class Nested
{
static Nested()
{
}
internal static readonly Singleton instance = new Singleton();
}
}
五 一个计数器的例子
一个计数器的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SingletonCounter
{
public class CountSingleton
{
static CountSingleton uniCounter = new CountSingleton();
private int totNum = 0;
private CountSingleton()
{
Thread.Sleep(2000);
}
static public CountSingleton Instance()
{
return uniCounter;
}
public void Add()
{
totNum++;
}
public int GetCounter()
{
return totNum;
}
}
public class CountMutilThread
{
public CountMutilThread()
{
}
public static void DoSomeWork()
{
string strResult = string.Empty;
CountSingleton myCounter = CountSingleton.Instance();
for (int i = 1; i <= 4; i++)
{
myCounter.Add();
strResult = "The thread" + Thread.CurrentThread.Name.ToString() + "------> " + "The counter is " + myCounter.GetCounter().ToString()+"\n";
Console.WriteLine(strResult);
}
}
public void StratMain()
{
Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 = new Thread(new ThreadStart(DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 = new Thread(new ThreadStart(DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 = new Thread(new ThreadStart(DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
DoSomeWork();
}
}
public class Client
{
public static void Main(string[] args)
{
CountMutilThread cmt = new CountMutilThread();
cmt.StratMain();
Console.ReadLine();
}
}
}
运行结果:
单例模式的应用范围很广,经常使用。
很多人可能在没学到设计模式时候已经使用到了。