C# 单例模式

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace MS
 8 {
 9     public class Singleton
10       {
11            private static Singleton m_instance = null;
12    
13            private Singleton()
14            {
15 
16            }
17            public static Singleton getInstance()
18            {
19                if(m_instance==null)
20                {
21                     m_instance=new Singleton();
22                }
23                return m_instance;
24           }
25       }
26     class Program
27     {
28         static void Main(string[] args)
29         {
30             Singleton s = Singleton.getInstance();
31             Singleton s1 = Singleton.getInstance();
32             if (s==s1)
33             {
34                 System.Console.WriteLine("相同");
35             }
36             
37             Console.ReadKey();
38         }
39     }
40 }

 

 

 单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点。”

posted @ 2014-02-08 11:26  kadajEvo  阅读(77)  评论(0)    收藏  举报