设计模式一般用来解决 从C#的Singleton设计模式实现看.NETFramework特性对开发者的重要性
从C#的Singleton设计模式实现看.NETFramework特性对开发者的重要性
近来 我在学习如何在C#语言中使用设计模式中读到一些资料 其中有关Singleton设计模式引起了我的注意
学过设计模式的开发者都知道Singleton模式 我想简要地解释一下这个设计模式是为那些尚未学习过设计模式的朋友 Singleton设计模式是告诉您如何在你的应用程序创建一个唯一类示例的全局对象 也就是说 这个对象只能被实例化一次 这个对象同时提供一个访问它的一个全局的访问点 这种对象如应用程序中的窗口管理器或者是打印假脱机 数据库连接池等
现在让我们先看一下设计模式一书中对Singleton模式的C++实现(为了方便比较 我把实现的代码写成内联的方式)
class Singleton
{
public:
static Singleton* Instance()
{
if(_instance== )
{
_instance=new Singleton;
}
return _instance;
}
protected:
Singleton();
Private:
Static Singleton* _instance;
};
Stington* Singleton::_instance= ;
这里我不想对上述的代码作解释 大家可以参阅本文后面提供的参考文献
为了更好的理解C# 我这里也出示一下Java中的Singleton模式实现代码(不是有人认为C#是对Java的剽窃吗J)
class Singleton
{
public Singleton Instance()
{
if(_instace==null)
{
synchronized (Class forName( Singleton ))
{
if (_instance == null)
{
_instance = new Singleton();
}
}
}
return _instance;
}

protected Singleton() {}
private static Singleton _instance = null;
}
在上述的Java代码中使用了双重检测机制 来避免多个线程的Singleton实例化问题 同样 我也不想对上述的代码做任何解释
首先 让我们看一下C#中类似Java的实现
class Singleton
{
public static Singleton Instance()
{
if(_instance==null)
{
lock (typeof(Singleton))
{
if(_instace==null)
{
_instance=new Singleton();
}
}
}
return _instance;
}
protected Singleton(){}
private static volatile Singleton _instance=null;
}
现在 是我们睁大眼睛的时候了 下面是利用 NET Framework平台优势实现Singleton模式的代码
sealed class Singleton
{
private Singleton();
public static readonly Singleton Instance=new Singleton();
}
非常吃惊是不是 不仅代码减少了许多 同时也解决了线程问题带来的性能上损失 有避免了编译器优化的问题 上述的代码真的能正常工作吗?如果能 那么它又是怎样工作的呢?
注意到 Singleton类被声明为sealed 以此保证它自己不会被继承 其次没有了Instance的方法 将原来_instance成员变量变成public readonly 并在声明时被初始化 通过这些改变 我们确实得到了Singleton的模式 原因是在JIT的处理过程中 如果类中的static属性被任何方法使用时 NET Framework将对这个属性进行初始化 于是在初始化Instance属性的同时Singleton类实例得以创建和装载 而私有的构造函数和readonly(只读)保证了Singleton不会被再次实例化 这正是Singleton设计模式的意图
lishixinzhi/Article/program/net/201311/12319