In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use the singleton pattern in web", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to use the singleton pattern in web" this article.
Introduction
Singleton pattern, as the name implies, has only one instance, and she is responsible for creating her own object. This class provides a way to access its unique object, which can be accessed directly without instantiating the object of the class. Let's take a look at some ways to implement it.
Mode of realization
1. Use lock to ensure that only one thread can enter the method.
/ singleton class / public class Singleton {private Singleton () {long lResult = 0; for (int I = 0; I < 100-000-000; iSum +) {lResult + = I;} Thread.Sleep (2000) Console.WriteLine ($"{this.GetType (). Name} complete the construction.");} private static Singleton Instance = null; private static readonly object Singleton_Lock = new object () Public static Singleton CreateInstance () {if (Instance = = null) {lock (Singleton_Lock) / / guarantee that only one thread can enter {Console.WriteLine ("enter lock queue...."); Thread.Sleep (1000) If (Instance = = null) Instance = new Singleton ();}} return Instance;} public static void DoNothing () {Console.WriteLine ("DoNothing") } public void Show () {Console.WriteLine ($"{this.GetType (). Name} Show....");}}
2. Use a static constructor, called by CLR, before the type is used for the first time, and only once.
/ singleton class / public class SingletonSecond {private SingletonSecond () {long lResult = 0; for (int I = 0; I < 100-000-000; iSum +) {lResult + = I;} Thread.Sleep (2000) Console.WriteLine ($"{this.GetType (). Name} completes the construction.");} private static SingletonSecond Instance = null; / static constructor, called by CLR, before the type is used for the first time, and only once! / / static SingletonSecond () {Instance = new SingletonSecond ();} public static SingletonSecond CreateInstance () {return Instance;} public static void DoNothing () {Console.WriteLine ("DoNothing") } public void Show () {Console.WriteLine ($"{this.GetType (). Name} Show....");}}
3. Use static fields, which are called by CLR, and initialize only once before the type is used for the first time.
/ singleton class / public class SingletonThird {private SingletonThird () {long lResult = 0; for (int I = 0; I < 100-000-000; iSum +) {lResult + = I;} Thread.Sleep (2000) Console.WriteLine ($"{this.GetType (). Name} completes the construction.");} / static fields, called by CLR, are initialized before the type is first used, and only once! / private static SingletonThird Instance = new SingletonThird (); public static SingletonThird CreateInstance () {return Instance;} public static void DoNothing () {Console.WriteLine ("DoNothing");} public int iNum = 0 Public void Show () {Console.WriteLine ($"{this.GetType () .Name} Show.. {iNum++}.");} public void Add () {this.iNum++;}}
Working with scen
1. An environment in which unique sequences need to be generated.
2. objects that need to be frequently instantiated and then destroyed.
3. Objects that take too much time or resources to create, but are often used.
4. A convenient environment for resources to communicate with each other.
The above is all the content of the article "how to use singleton pattern in web". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.