单例模式 LAZY:
interface IDatabase
{
int GetConnection(string name);
}
class FileDatabase : IDatabase
{
private Dictionary<string, int> dic = new Dictionary<string, int>();
private FileDatabase()
{
count++;
Console.WriteLine("Read text.");
dic = File.ReadAllLines("Demo.txt").Batch(2).ToDictionary(
list => list.ElementAt(0).Trim(),
list => int.Parse(list.ElementAt(1).Trim())
);
}
public int GetConnection(string name)
{
return dic[name];
}
public int GetDictionaryCount()
{
return dic.Count;
}
private static Lazy database = new(() => { Console.WriteLine("Load...");return new FileDatabase(); });
public static FileDatabase Instance => database.Value;
private static int count;
public static int Count => count;
}