Browse Source

泛型单例模式的实现

jeffrey 9 years ago
parent
commit
837a6e793b

+ 1 - 1
FactoryPattern/AbstractBus.cs

@@ -8,7 +8,7 @@ namespace FactoryPattern
 
         public void GetInfo()
         {
-            Console.WriteLine($"I am {this.GetType().Name}.");
+            Console.WriteLine(string.Format("I am {0}.",this.GetType().Name));
         }
     }
 

+ 1 - 1
FactoryPattern/AbstractCar.cs

@@ -12,7 +12,7 @@ namespace FactoryPattern
 
         public void GetInfo()
         {
-            Console.WriteLine($"I am {this.GetType().Name}.");
+            Console.WriteLine(string.Format("I am {0}.",this.GetType().Name));
         }
     }
 

+ 40 - 0
SingletonPattern/GenericSingleton.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SingletonPattern
+{
+    public class GenericSingleton<T> where T : class//,new ()
+    {
+        private static T instance;
+
+        private static readonly object Locker = new object();
+
+        public static T GetInstance()
+        {
+            //没有第一重 instance == null 的话,每一次有线程进入 GetInstance()时,均会执行锁定操作来实现线程同步,
+            //非常耗费性能 增加第一重instance ==null 成立时的情况下执行一次锁定以实现线程同步
+            if (instance == null)
+            {
+                //Double-Check Locking 双重检查锁定
+                lock (Locker)
+                {
+                    if (instance == null)
+                    {
+                        //instance = new T();
+                        //需要非公共的无参构造函数,不能使用new T() ,new不支持非公共的无参构造函数 
+                        instance = Activator.CreateInstance(typeof(T), true) as T;//第二个参数防止异常:“没有为该对象定义无参数的构造函数。”
+                    }
+                }
+            }
+            return instance;
+        }
+
+        public void GetInfo()
+        {
+            Console.WriteLine(string.Format("I am {0}.", this.GetType().Name));
+        }
+    }
+}

+ 16 - 1
SingletonPattern/Program.cs

@@ -15,6 +15,7 @@ namespace SingletonPattern
             Console.WriteLine("单例模式:");
             TestStaticSingleton();
             TestLasyInitialSingleton();
+            TestGenericSingleton();
             TestDoubleLockSingleton();
         }
 
@@ -43,7 +44,7 @@ namespace SingletonPattern
         {
             Console.WriteLine("锁机制确保多线程只产生一个实例");
 
-            for (int i = 0; i < 8; i++)
+            for (int i = 0; i < 2; i++)
             {
                 Thread thread=new Thread(ExecuteInForeground);
 
@@ -63,5 +64,19 @@ namespace SingletonPattern
             singleton3.GetInfo();
             Console.WriteLine(singleton3.GetHashCode());
         }
+
+        private static void TestGenericSingleton()
+        {
+            Console.WriteLine("泛型单例模式:");
+
+            Singleton4 instance = GenericSingleton<Singleton4>.GetInstance();
+
+            instance.GetInfo();
+
+            var singleton4 = Singleton4.Instance;
+
+            singleton4.GetInfo();
+            Console.ReadLine();
+        }
     }
 }

+ 1 - 1
SingletonPattern/Singleton1.cs

@@ -29,7 +29,7 @@ namespace SingletonPattern
 
         public void GetInfo()
         {
-            Console.WriteLine($"I am {this.GetType().Name}.");
+            Console.WriteLine(string.Format("I am {0}.",this.GetType().Name));
         }
     }
 }

+ 1 - 1
SingletonPattern/Singleton2.cs

@@ -37,7 +37,7 @@ namespace SingletonPattern
 
         public void GetInfo()
         {
-            Console.WriteLine($"I am {this.GetType().Name}.");
+            Console.WriteLine(string.Format("I am {0}.",this.GetType().Name));
         }
     }
 }

+ 4 - 1
SingletonPattern/Singleton3.cs

@@ -20,10 +20,13 @@ namespace SingletonPattern
 
         public static Singleton3 Instance()
         {
+            //没有第一重 instance == null 的话,每一次有线程进入 GetInstance()时,均会执行锁定操作来实现线程同步,
+            //非常耗费性能 增加第一重instance ==null 成立时的情况下执行一次锁定以实现线程同步
             if (_instance==null)
             {
                 lock (Locker)
                 {
+                    //Double-Check Locking 双重检查锁定
                     if (_instance==null)
                     {
                         _instance = new Singleton3();
@@ -36,7 +39,7 @@ namespace SingletonPattern
 
         public void GetInfo()
         {
-            Console.WriteLine($"I am {this.GetType().Name}.");
+            Console.WriteLine(string.Format("I am {0}.",this.GetType().Name));
         }
     }
 }

+ 40 - 0
SingletonPattern/Singleton4.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SingletonPattern
+{
+    /// <summary>
+    /// 通过继承泛型单例来获取实例
+    /// </summary>
+    public class Singleton4
+    {
+        /// <summary>
+        /// 非公共无参构造函数,确保该类无法在其他地方实例化
+        /// </summary>
+        private Singleton4()
+        {
+
+        }
+
+        /// <summary>
+        /// 也可以通过暴露属性获取实例
+        /// </summary>
+        public static Singleton4 Instance
+        {
+            get
+            {
+                return GenericSingleton<Singleton4>.GetInstance();
+            }
+        }
+
+        public void GetInfo()
+        {
+            Console.WriteLine(string.Format("I am {0}.", this.GetType().Name));
+        }
+
+    }
+   
+}

+ 2 - 0
SingletonPattern/SingletonPattern.csproj

@@ -43,11 +43,13 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="GenericSingleton.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Singleton1.cs" />
     <Compile Include="Singleton2.cs" />
     <Compile Include="Singleton3.cs" />
+    <Compile Include="Singleton4.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />