只生一个孩子好!~
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;
}
}
}
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;
}
}
}
相关文章推荐
- [转]Digg:一个孩子18个月挣6000万美元
- 一个中国孩子在美国学校的经历
- 特别喜欢的一首顾城的作品我是一个任性的孩子
- 毁掉一个孩子的几个方法 有多少家长正在这么做?
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
- 一个很有趣的孩子
- 我——一个涉世不久的孩子的软件路
- 孩子,我首先希望你自始至终都是一个理想主义者
- 一个孩子的日记
- 有n个孩子站成一圈,从第一个孩子开始顺时针方向报数,报到3的人出列,下一个人继续从1报数,直到最后剩下一个孩子为止。问剩下第几个孩子。
- 孩子,我首先希望你自始至终都是一个理想主义者
- 作为一个大学才开始入门学计算机编程的孩子想要的东西-----听我扯,你蛋疼,他菊紧,我开心
- 孩子,我首先希望你自始至终都是一个理想主义者。
- 如何教出一个优秀的孩子
- " 一个鸡蛋”改变TA的世界——让贫困地区的孩子每天都能吃上一个鸡蛋
- 随心篇第七期:我是一个爱玩游戏的孩子
- 在一个重男轻女的国家里,每个家庭都想生男孩,如果他们生的孩子是女孩,就再生一个,直到生下的是男孩为止。这样的国家,男女比例会是多少?
- 一个中国孩子在美国学校的经历(上)
- 老王有两个孩子,已知至少有一个孩子是在星期二出生的男孩。问:两个孩子都是男孩的概率是多大?
- 毛毛最新推荐伤感日志:我是一个任性的孩子