In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of the sample analysis of the C# singleton pattern. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Singleton pattern is also a kind of creative pattern, and it is also a relatively simple one of the 23 design patterns. See the name means that in the whole software system, there is only one object of a certain type, and there is only one place to access it, that is, there is only one global object access point, and this instance or object is shared by all applications. Many functional modules can be used, such as database connection pool objects and printer objects, because in the whole system, the connection to the database is only in one place, and the printer is only one in the whole system. In this case, the singleton pattern greatly reduces the memory overhead, because the creation of the object consumes memory, and because there is only one instance in the system, it is easier to control and omits the process of object creation. a faster response, but when using the singleton pattern, because there is only one instance, all threads can use this instance Then we can not guarantee the safety of the thread, if we want to ensure the safety of the thread, we need to use some other auxiliary measures. Therefore, it is best not to use singleton mode for thread-safe objects, otherwise it may reduce the efficiency of the system. The singleton pattern can be implemented with only one class and associate itself with itself, so how can it be implemented? We just need to define the constructor as private so that the user cannot create the object himself, and then hold a unique instance of the class through a static method and static variable.
Example:
Create a singleton class:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace singleton mode {public class Singleton {/ 1. Define a private constructor that cannot be called outside the class / private Singleton () {Console.WriteLine ("I was created!");} / / 2. Create a static variable private stati c Singleton Instance; / / 3 for this class. Create a static method whose return value is this type (this method is public) public static Singleton CreateInstance () {if (Instance = = null) {Instance = new Singleton ();} return Instance;}
The main program calls:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace singleton pattern {class Program {static void Main (string [] args) {Singleton S1 = Singleton.CreateInstance (); Singleton S2 = Singleton.CreateInstance (); Console.ReadKey ();}
Running result:
As you can see from the final result, the instance was created only once.
Take a look at the following example:
For (int I = 0; I
< 10; i++){ // 执行委托的异步调用 new Action(() =>{Singleton singleton = Singleton.CreateInstance ();}) .BeginInvoke (null,null);}
Results:
From the screenshot above, you can see that the constructor was executed three times. Isn't the singleton pattern only constructed once? Why has it been executed three times? Because this is multithreaded concurrency, 10 tasks start at the same time, and multiple threads may enter when the object is not null, so it will be executed multiple times. To solve this problem, you can use locking.
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace singleton mode {public class Singleton {/ 1. Define a private constructor that cannot be called outside the class / private Singleton () {Console.WriteLine ("I was created!");} / / 2. Create a static variable private static Singleton Instance; / / create the lock private static object Singleton_Lock = new object (); / / 3. Create a static method whose return value is of this type (this method is public) public static Singleton CreateInstance () {lock (Singleton_Lock) / / ensure that only one thread can enter the judgment {if (Instance = = null) {Instance = new Singleton () at any time. }} return Instance;}
Run the program again to see the results:
At this point, only one object will be constructed.
Let's take a look at the lazy singleton pattern:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace singleton pattern {public class SingletonSecond {private SingletonSecond () {Console.WriteLine ("I was created!");} private static SingletonSecond Instance = null / static constructor: CLR ensures that / static SingletonSecond () {Instance = new SingletonSecond ();} public static SingletonSecond CreateInstance () {return Instance;}} is called only once before the first use of this class.
Let's take a look at the third way of writing:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace singleton pattern {public class SingletonThird {private SingletonThird () {Console.WriteLine ("I was created!") } / static variable: it is initialized when the type is used for the first time, and only once / private static SingletonThird Instance = new SingletonThird (); public static SingletonThird CreateInstance () {return Instance;} Thank you for reading! This is the end of this article on "sample Analysis of C# singleton pattern". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.