C# singleton mode lazy / hungry

Before using a design pattern, you must first understand the advantages of using it:

1. The singleton mode is to ensure that in the entire application life cycle, at any time, the specified class has only one instance object, which reduces the creation of objects, thereby reducing the overhead of program memory.

2. The singleton pattern is a commonly used software design pattern. In its core structure it contains only a special class called a singleton. The singleton mode can ensure that there is only one instance of a class in the system and the instance is easy to access from the outside world, thereby facilitating the control of the number of instances and saving system resources. If you want to have only one object of a certain class in the system, the singleton pattern is the best solution.
3. To put it bluntly, it is to ensure that a class has only one instance and provide a global access point to the instance.

Application scenarios:
1. The Task Manager of Windows is a typical singleton mode, and only one window can be opened at any time.
  2. Windows' Recycle Bin (recycle bin) is also a typical singleton application. During the whole system operation, the recycle bin maintains only one instance.


  3. The counter of the website is generally implemented in a singleton mode, otherwise it is difficult to synchronize.
  4. The log application of the application is generally implemented in the singleton mode. This is generally because the shared log file is always open, because there can only be one instance to operate, otherwise the content is not easy to append.
5. The reading of the configuration object of the web application generally also applies the singleton mode, this is because the configuration file is a shared resource.
  6. The design of the database connection pool generally adopts the singleton mode, because the database connection is a kind of database resource. The use of database connection pools in database software systems is mainly to save the efficiency loss caused by opening or closing database connections. This efficiency loss is still very expensive, because the use of singleton mode for maintenance can greatly reduce this loss. .
  7. The design of multi-threaded thread pools generally adopts the singleton mode, because the thread pool needs to facilitate the control of the threads in the pool.
  8. The file system of the operating system is also a specific example of the implementation of the large singleton pattern. An operating system can only have one file system.

Code implementation (lazy mode: instantiate yourself when it is referenced for the first time):
1. Define the constructor of the class as a private method, so that other code cannot instantiate the class by calling the constructor of the class The object of the class, the only instance of the class can be obtained only through the static methods provided by the class; (to prevent the caller from directly instantiating)

2. Provide a static method in the class. When we call this method, if the reference held by the class is not null, the reference will be returned. If the reference held by the class is null, an instance of the class will be created and the reference of the instance will be created. Gives this class a held reference.
3. Create a visitor object (for locking to prevent concurrency)

4. Create a global access point (output instance): so the method must not be private or protected, otherwise other classes cannot call the method, and it must be a static method because the constructor of this class is

Private externals cannot instantiate this class (as mentioned earlier), so the method can only be called through the class name. (dot) method name. Otherwise, the class you are creating is a waste class and cannot be used by people

Use double locks to prevent concurrency issues with multithreaded calls:

When Single is null and two threads call
the GetInstance method at the same time, they will both pass
the first judgment of Single==null, and then due to the lock
mechanism, only one of the two threads will enter, and the
other queue up outside. One must enter
and come out before the other can enter. At this time, if there is
no judgment on whether the second single is null,
the first thread creates an instance, and the second thread
can continue to create new instances, which does not
achieve the purpose of singleton.

5. Invoke

(Hungry mode: instantiate yourself when loading)

Note : At this time, the service object has been assigned an instance of this class when the program is loaded (but only assigned once, because static readonly is assigned using a static constructor)

Such an implementation is similar to the previous example, and also solves two basic problems solved by the singleton pattern view: global access and instantiation control, and public static properties provide a global access point for accessing instances. The difference is that it relies on the common language runtime to initialize variables. Because the constructor is private, the Singleton class cannot be instantiated outside of the class itself; therefore, the variable refers to the only instance that can exist in the system. Note, though, that instance variables are marked readonly, which means they can only be assigned during static initialization or in constructors. Since this static initialization method is to instantiate itself when it is loaded, it is vividly called a hungry Chinese-style singleton class. The original singleton mode processing method is to be referenced for the first time. It will instantiate itself, so it is called a lazy singleton class.

Due to the hungry Chinese style, that is, the static initialization method, it is an object that is instantiated as soon as the class is loaded, so it needs to occupy system resources in advance. Then the lazy person will face the security problem of multi-threaded access, and it needs to do double locking to ensure security. So which method to use depends on the actual needs. From the point of view of the C# language, the hungry Chinese-style singleton class is enough to meet our needs.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324815132&siteId=291194637