public void GetEagerSingleton() { // Locks the creation of object to 1 thread, but is very expensive. EagerSingleton eagerSingleton = EagerSingleton.GetInstance(); Assert.IsInstanceOfType(eagerSingleton, typeof(EagerSingleton)); }
static void Main(string[] args) { Console.WriteLine("饿汉式 单例类 的处理!"); for (int i = 0; i < 10; i++) { EagerSingleton data = EagerSingleton.GetInstance(); Console.WriteLine(data.DemoCount++); } Console.WriteLine("懒汉式 单例类 的处理!"); for (int i = 0; i < 10; i++) { LazySingleton data = LazySingleton.GetInstance(); Console.WriteLine(data.DemoCount++); } Console.ReadLine(); }
void static Main(string[] args) { EagerSingleton.GetInstance(); }
/// <summary> /// 单例模式测试 /// </summary> static void Singleton() { EagerSingleton.GetInstance().Print(); LazySingleton.Instance.Print(); }