单例模式
在日常编程中,实例化必不可少,有一些特殊的类,需要保证在系统中只存在一个实例,才能确保它们的逻辑正确性,以及良好的效率。这种系统中只存在一个实例化的类,通常可以使用单例模式来完成。
这是一个普通的类以及实例化:
Singleton.cs
using System;
using System.Threading;
namespace SingletonPattern
{
public class Singleton
{
private Singleton()
{
Thread.Sleep(1000);
string data = "这里需存储1GB数据";
string resource = "这里需要加载1GB资源文件";
Console.WriteLine("{0}:,线程ID{1}, 加载数据:{2},加载资源:{3}", this.GetType().Name, Thread.CurrentThread.ManagedThreadId, data, resource);
}
public void Load()
{
Console.WriteLine("Show方法被调用:{0}", this.GetType().Name);
}
}
}
新建3个类,在类中声明Singleton
Class1.cs:
using System;
namespace SingletonPattern
{
public class SubClass1
{
public static void SubSingleton()
{
Console.WriteLine("Class1");
Singleton singleton = new Singleton();
singleton.Load();
}
}
}
Class2.cs:
using System;
namespace SingletonPattern
{
public class SubClass2
{
public static void SubSingleton()
{
Console.WriteLine("Class2");
Singleton singleton = new Singleton();
singleton.Load();
}
}
}
Class3.cs:
using System;
namespace SingletonPattern
{
public class SubClass3
{
public static void SubSingleton()
{
Console.WriteLine("Class3");
Singleton singleton = new Singleton();
singleton.Load();
}
}
}
Program.cs调用:
using System;
namespace SingletonPattern
{
internal class Program
{
private static void Main(string[] args)
{
//第一种情况
for (int i = 0; i < 10; i++)
{
Singleton singleton = new Singleton();
singleton.Load();
}
//第二种情况
SubClass1.SubSingleton();
SubClass2.SubSingleton();
SubClass3.SubSingleton();
Console.ReadKey();
}
}
}
如果需要类只实例一次,第一种情况我们可以在外部实例化,第二种情况,可以将实例化的类当作参数传入。但是做项目不是一个人在做,难免有人在外部重复的做实例化的操作。这种情况下就需要使用单例模式。
单线程模式下:
Singleton.cs
===============================================================================
using System;
using System.Threading;
namespace SingletonPattern
{
public class Singleton
{
private Singleton()
{
Thread.Sleep(1000);
string data = "这里需存储1GB数据";
string resource = "这里需要加载1GB资源文件";
Console.WriteLine("{0}:,线程ID{1}, 加载数据:{2},加载资源:{3}", this.GetType().Name, Thread.CurrentThread.ManagedThreadId, data, resource);
}
public void Load()
{
Console.WriteLine("这里被调用了{0}.show", this.GetType().Name);
}
private static Singleton singleton;
public static Singleton createInstance()
{
if (singleton == null)
{
singleton = new Singleton();
}
return singleton;
}
}
}
===================================================================================
Program.cs
===================================================================================
using System;
namespace SingletonPattern
{
internal class Program
{
//public static Singleton singleton = Singleton.CreateInstance();
private static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Singleton singleton = Singleton.createInstance();
singleton.Load();
}
Console.ReadKey();
}
}
}
多线程模式下:
Singleton.cs
===============================================================================
using System;
using System.Threading;
namespace SingletonPattern
{
/// <summary>
/// 私有化构造函数
/// 私有的静态变量
/// 一个静态的对象创建方法
/// </summary>
public class Singleton
{
private Singleton()
{
Thread.Sleep(1000);
string data = "这里需存储1GB数据";
string resource = "这里需要加载1GB资源文件";
Console.WriteLine("{0}:,线程ID{1}, 加载数据:{2},加载资源:{3}", this.GetType().Name, Thread.CurrentThread.ManagedThreadId, data, resource);
}
public void Load()
{
Console.WriteLine("这里被调用了{0}.show", this.GetType().Name);
}
private static Singleton singleton;
private static object _lock = new object();
public static Singleton createInstance()
{
lock (_lock)
{
if (singleton == null)
{
singleton = new Singleton();
}
}
return singleton;
}
}
}
===================================================================================
Program.cs
===================================================================================
using System;
namespace SingletonPattern
{
internal class Program
{
//public static Singleton singleton = Singleton.CreateInstance();
private static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
new Action(() =>
{
Singleton singleton = Singleton.createInstance();
singleton.Load();
}).BeginInvoke(null, null);
}
Console.ReadKey();
}
}
}
将构造函数私有化,通过一个静态方法来做实例化的操作。