CSharp: Singleton Pattern in donet core 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | /// <summary> /// 单例模式 Singleton Pattern /// geovindu,Geovin Du edit /// </summary> public class Database { /// <summary> /// /// </summary> private Database() { } /// <summary> /// /// </summary> public static Database Instance { get ; } = new Database(); } /// <summary> /// /// </summary> public class MyDatabase { /// <summary> /// /// </summary> private MyDatabase() { Console.WriteLine( "Initializing database" ); } /// <summary> /// /// </summary> private static Lazy<MyDatabase> instance = new Lazy<MyDatabase>(() => new MyDatabase()); /// <summary> /// /// </summary> public static MyDatabase Instance => instance.Value; } /// <summary> /// /// </summary> public interface IDatabase { int GetPopulation( string name); } /// <summary> /// /// </summary> public class SingletonDatabase : IDatabase { /// <summary> /// /// </summary> private Dictionary< string , int > capitals; /// <summary> /// /// </summary> private static int instanceCount; /// <summary> /// /// </summary> public static int Count => instanceCount; /// <summary> /// /// </summary> private SingletonDatabase() { Console.WriteLine( "Initializing database" ); capitals = File.ReadAllLines( Path.Combine( new FileInfo( typeof (IDatabase).Assembly.Location) .DirectoryName, "capitals.txt" ) ) .Batch(2) .ToDictionary( list => list.ElementAt(0).Trim(), list => int .Parse(list.ElementAt(1))); } /// <summary> /// /// </summary> /// <param name="name"></param> /// <returns></returns> public int GetPopulation( string name) { return capitals[name]; } /// <summary> /// laziness + thread safety /// </summary> private static readonly Lazy<SingletonDatabase> instance = new Lazy<SingletonDatabase>(() => { instanceCount++; return new SingletonDatabase(); }); public static IDatabase Instance => instance.Value; } /// <summary> /// /// </summary> public class SingletonRecordFinder { /// <summary> /// /// </summary> /// <param name="names"></param> /// <returns></returns> public int TotalPopulation(IEnumerable< string > names) { return names.Sum(name => SingletonDatabase.Instance.GetPopulation(name)); } } /// <summary> /// /// </summary> public class ConfigurableRecordFinder { /// <summary> /// /// </summary> private IDatabase database; /// <summary> /// /// </summary> /// <param name="database"></param> public ConfigurableRecordFinder(IDatabase database) { this .database = database; } /// <summary> /// /// </summary> /// <param name="names"></param> /// <returns></returns> public int GetTotalPopulation(IEnumerable< string > names) { return names.Sum(name => database.GetPopulation(name)); } } /// <summary> /// /// </summary> public class DummyDatabase : IDatabase { /// <summary> /// /// </summary> /// <param name="name"></param> /// <returns></returns> public int GetPopulation( string name) { return new Dictionary< string , int > { [ "alpha" ] = 1, [ "beta" ] = 2, [ "gamma" ] = 3 }[name]; } } /// <summary> /// /// </summary> public class OrdinaryDatabase : IDatabase { /// <summary> /// /// </summary> private readonly Dictionary< string , int > cities; /// <summary> /// /// </summary> public OrdinaryDatabase() { Console.WriteLine( "Initializing database" ); cities = File.ReadAllLines( Path.Combine( new FileInfo( typeof (IDatabase).Assembly.Location) .DirectoryName, "capitals.txt" ) ) .Batch(2) .ToDictionary( list => list.ElementAt(0).Trim(), list => int .Parse(list.ElementAt(1))); } /// <summary> /// /// </summary> /// <param name="name"></param> /// <returns></returns> public int GetPopulation( string name) { return cities[name]; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /// <summary> /// 单例模式 Singleton Pattern /// geovindu,Geovin Du edit /// </summary> public sealed class Singleton { #region Singleton implementation using static constructor //The following line is discussed in analysis section. //private static readonly Singleton Instance = new Singleton(); private static readonly Singleton Instance; private static int TotalInstances; /* * Private constructor is used to prevent * creation of instances with 'new' keyword * outside this class. */ /// <summary> /// /// </summary> private Singleton() { Console.WriteLine( "--Private constructor is called." ); Console.WriteLine( "--Exit now from private constructor." ); } /* * A static constructor is used for the following purposes: * 1. To initialize any static data; * 2. To perform a specific action only once. * * The static constructor will be called automatically before * * i. You create the first instance; or * ii.You refer to any static members in your code. * */ /// <summary> /// Here is the static constructor /// </summary> static Singleton() { // Printing some messages before you create the instance Console.WriteLine( "-Static constructor is called." ); Instance = new Singleton(); TotalInstances++; Console.WriteLine($ "-Singleton instance is created.Number of instances:{ TotalInstances}" ); Console.WriteLine( "-Exit from static constructor." ); } /// <summary> /// /// </summary> public static Singleton GetInstance { get { return Instance; } } /* * If you like to use expression-bodied read-only * property, you can use the following line (C# v6.0 onwards). */ //public static Singleton GetInstance => Instance; #endregion /* The following line is used to discuss the drawback of the approach. */ /// <summary> /// /// </summary> public static int MyInt = 25; } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Console.WriteLine( "***单例模式 Singleton Pattern Demonstration.***\n" ); /*The following line is used to discuss the drawback of the approach.*/ //Console.WriteLine($"The value of MyInt is :{Singleton.MyInt}"); // Private Constructor.So,you cannot use the 'new' keyword. //Singleton s = new Singleton(); // error Console.WriteLine( "Trying to get a Singleton instance, called firstInstance." ); Singleton firstInstance = Singleton.GetInstance; Console.WriteLine( "Trying to get another Singleton instance, called secondInstance." ); Singleton secondInstance = Singleton.GetInstance; if (firstInstance.Equals(secondInstance)) { Console.WriteLine( "The firstInstance and secondInstance are the same." ); } else { Console.WriteLine( "Different instances exist." ); } // var rf = new SingletonRecordFinder(); var names = new [] { "Seoul" , "Mexico City" }; var ci = names[0].ToString(); int tp = rf.TotalPopulation(names); Console.WriteLine(tp); var db = new DummyDatabase(); var rf2 = new ConfigurableRecordFinder(db); int t= rf2.GetTotalPopulation( new [] { "alpha" , "gamma" }); ; Console.WriteLine(t); db.GetPopulation( "gamma" ); var db2 = SingletonDatabase.Instance; // works just fine while you're working with a real database. var city = "Tokyo" ; Console.WriteLine($ "{city} has population {db2.GetPopulation(city)}" ); Console.WriteLine($ "{names[0]} has population {db2.GetPopulation(names[0])}" ); Console.Read(); |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ***单例模式 Singleton Pattern Demonstration.*** Trying to get a Singleton instance, called firstInstance. -Static constructor is called. --Private constructor is called. --Exit now from private constructor. -Singleton instance is created.Number of instances:1 -Exit from static constructor. Trying to get another Singleton instance, called secondInstance. The firstInstance and secondInstance are the same. Initializing database 34900000 4 Tokyo has population 33200000 Seoul has population 17500000 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!