惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
J
Java Code Geeks
Jina AI
Jina AI
罗磊的独立博客
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
D
DataBreaches.Net
博客园 - 叶小钗
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
B
Blog
V
Visual Studio Blog
雷峰网
雷峰网
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - walker

[转]有一种爱叫索取 悟空,真的是空? Interpreter解释器(行为型模式) Command命令(行为型模式) TemplateMethod模板方法(行为型模式) Proxy代理(结构型模式) Flyweight享元(结构型模式) Facade外观(结构型模式) Decorator装饰(结构型模式) Composite组合(结构型模式) Bridge桥接(结构型模式) Adapter适配器(结构型模式) Prototype原型(创建型模式) Builder生成器(创建型模式) AbstractFactory抽象工厂(创建型模式) FactoryMethod工厂方法(创建型模式) 一个C#睡前故事 C#中得到网卡号 C#怎样打开关闭CDROM?
Singleton单件(创建型模式)
walker · 2006-08-17 · via 博客园 - walker

单件模式要求一个类有且仅有一个实例.
 Singleon类:

 1using System;
 2
 3namespace Singleton
 4{
 5    /// <summary>
 6    /// Singleton 的摘要说明。
 7    /// </summary>

 8    public class Singleton
 9    {
10     
11            private static Singleton _factory; 
12            private static Object _classLock = typeof(Singleton);
13            private long _wipMoves;
14            private Singleton()
15            {
16                _wipMoves = 0;
17            }

18            public static Singleton GetFactory()
19            {
20                lock (_classLock)
21                {
22                    if (_factory == null)
23                    {
24                        _factory = new Singleton();
25                    }

26                    return _factory;
27                }

28            }

29            public void RecordWipMove()
30            {
31                lock (_classLock)
32                {
33                    _wipMoves++;
34                }

35            }

36            // for the Aster star press example; not implemented
37            public void CollectPaste() 
38            {
39                Console.Write(_wipMoves+"\n");
40            }

41
42            /// <summary>
43            /// Return an example list of "up" machines, supporting "ShowConcurrentWhile"
44            /// and other examples).
45            /// </summary>
46            /// <returns>an example list of "up" machines</returns>

47             
48        }

49    }

50
51 
52

主程序:

 1using System;
 2
 3namespace Singleton
 4{
 5    /// <summary>
 6    /// Class1 的摘要说明。
 7    /// </summary>

 8    class Class1
 9    {
10        /// <summary>
11        /// 应用程序的主入口点。
12        /// </summary>

13        [STAThread]
14        static void Main(string[] args)
15        {
16            //
17            // TODO: 在此处添加代码以启动应用程序
18            //
19
20            Singleton singleton =Singleton.GetFactory();
21            singleton.RecordWipMove();
22            singleton.RecordWipMove();
23            
24            Singleton singleton1 =Singleton.GetFactory();
25            singleton1.RecordWipMove();
26            singleton1.RecordWipMove();
27        
28            singleton.CollectPaste();
29            singleton1.CollectPaste();
30            Console.WriteLine(object.ReferenceEquals(singleton, singleton1) == true);
31
32            Console.ReadLine();
33        }

34    }

35}

36