设计模式---单例模式(singleton)

设计模式—单例模式(singleton)

c56fe019dbd71a62dbb4bd42.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Singleton
{
private static Singleton s;

private Singleton()
{
}

public static Singleton GetInstance()
{
if (s == null)
{
s = new Singleton();
}

return s;
}
}

class Client
{
public static void Main()
{
Singleton s1 = Singleton.GetInstance();
Singleton s2 = Singleton.GetInstance();
if (object.ReferenceEquals(s1, s2))
{
Console.WriteLine("一样的");
Console.WriteLine(s1);
}

Console.Read();
}
}
}