设计模式——单键模式
名称 | Singleton |
结构 | ![]() |
意图 | 保证一个类仅有一个实例,并提供一个访问它的全局访问点。 |
适用性 |
|
1
// Singleton
2![](/Images/OutliningIndicators/None.gif)
3
// Intent: "Ensure a class only has one instance, and provide a global
4
// point of access to it".
5![](/Images/OutliningIndicators/None.gif)
6
// For further information, read "Design Patterns", p127, Gamma et al.,
7
// Addison-Wesley, ISBN:0-201-63361-2
8![](/Images/OutliningIndicators/None.gif)
9
/* Notes:
10
* If it makes sense to have only a single instance of a class (a so-called
11
* singleton), then it makes sense to enforce this (to elimintate potential
12
* errors, etc).
13
*
14
* A class based on the singleton design pattern protects its constructor,
15
* so that only the class itself (e.g. in a static method) may instantiate itself.
16
* It exposes an Instance method which allows client code to retrieve the
17
* current instance, and if it does not exist to instantiate it.
18
*/
19
20
namespace Singleton_DesignPattern
21
{
22
using System;
23![](/Images/OutliningIndicators/InBlock.gif)
24
class Singleton
25
{
26
private static Singleton _instance;
27
28
public static Singleton Instance()
29
{
30
if (_instance == null)
31
_instance = new Singleton();
32
return _instance;
33
}
34
protected Singleton(){}
35![](/Images/OutliningIndicators/InBlock.gif)
36
// Just to prove only a single instance exists
37
private int x = 0;
38
public void SetX(int newVal) {x = newVal;}
39
public int GetX(){return x;}
40
}
41![](/Images/OutliningIndicators/InBlock.gif)
42
/// <summary>
43
/// Summary description for Client.
44
/// </summary>
45
public class Client
46
{
47
public static int Main(string[] args)
48
{
49
int val;
50
// can't call new, because constructor is protected
51
Singleton FirstSingleton = Singleton.Instance();
52
Singleton SecondSingleton = Singleton.Instance();
53![](/Images/OutliningIndicators/InBlock.gif)
54
// Now we have two variables, but both should refer to the same object
55
// Let's prove this, by setting a value using one variable, and
56
// (hopefully!) retrieving the same value using the second variable
57
FirstSingleton.SetX(4);
58
Console.WriteLine("Using first variable for singleton, set x to 4");
59![](/Images/OutliningIndicators/InBlock.gif)
60
val = SecondSingleton.GetX();
61
Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val);
62
return 0;
63
}
64
}
65
}
66![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
19
![](/Images/OutliningIndicators/None.gif)
20
![](/Images/OutliningIndicators/None.gif)
21
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
64
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
66
![](/Images/OutliningIndicators/None.gif)