您的位置:首页 > 其它

只生一个孩子好!~

2008-09-03 09:27 169 查看
摘自大话设计模式

Code

using System;

using System.Collections.Generic;

using System.Text;

namespace 单例模式

{

class Program

{

static void Main(string[] args)

{

Singleton s1 = Singleton.GetInstance();

Singleton s2 = Singleton.GetInstance();

if (s1 == s2)

{

Console.WriteLine("Objects are the same instance");

}

Console.Read();

}

}

//class Singleton

//{

// private static Singleton instance;

// private static readonly object syncRoot = new object();

// private Singleton()

// {

// }

// public static Singleton GetInstance()

// {

// if (instance == null)

// {

// lock (syncRoot)

// {

// if (instance == null)

// {

// instance = new Singleton();

// }

// }

// }

// return instance;

// }

//}

public sealed class Singleton

{

private static readonly Singleton instance = new Singleton();

private Singleton() { }

public static Singleton GetInstance()

{

return instance;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐