Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the .net singleton pattern

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is the .net singleton pattern". In the daily operation, I believe that many people have doubts about what the .net singleton pattern is. The editor consulted all kinds of data and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what is the .net singleton pattern"! Next, please follow the editor to study!

Each computer can have several printers, but only one Printer Spooler to prevent two print jobs from being output to the printer at the same time. Each computer can have several fax cards, but only one software should be responsible for managing fax cards to avoid two fax jobs being sent to the fax card at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent one communication port from being called by two requests at the same time.

Problem description:

Singleton mode Singleton Pattern

Problem solving:

(1) brief introduction of singleton model:

The Singleton pattern requires one and only one instance of a class and provides a global access point. This raises the question: how do you bypass regular constructors and provide a mechanism to ensure that a class has only one instance? When a client calls a class, it does not consider whether there is only one instance of the class, so it should be the responsibility of the class designer, not the user of the class.

Singleton pattern features:

(1.1) one and only one instance of a class

(1.2) the class provides a global access point

(2) the realization of singleton mode:

(2.1) simple implementation:

The advantages of the above implementation:

(1) the initialization of the instance is not carried out until the object is required. This implementation method is called "lazy instantiation", which avoids creating unnecessary Singleton when the program starts.

Shortcomings of the above implementation:

(1) this kind of implementation is not safe for multithreaded environment, it is possible to instantiate multiple objects, for example, there may be two threads to judge instance==null, and then create two singleton objects, which goes against the design intention of singleton pattern.

(2.2) safe threads:

The above implementation ensures that only one singleton object will be created in the case of multithreading, but padlock adds additional overhead

(2.3) double locking

This implementation is safe for multithreading, and the thread is not locked every time, but only when it is determined that the object instance has not been created. With our analysis in the first part above, we know that after locking, we have to judge whether the object has been created or not. It solves the problem of thread concurrency while avoiding exclusive locking in calls to get Instance. It also allows you to delay instantiation until the first time the object is accessed. In fact, applications rarely need this type of implementation. In most cases, we use static initialization. This approach still has many disadvantages: delayed initialization cannot be achieved.

(2.4) static initialization

In this implementation, an instance is created the first time any member of the class is referenced, which is marked as sealed to prevent derivation from occurring, while derivation may increase the instance. In addition, variables are marked readonly, which means that variables can only be assigned during static initialization (the example shown here) or in the class constructor. It can still be used to solve two basic problems that the Singleton pattern tries to solve: global access and instantiation control. Public static properties provide a global access point for accessing the instance. In addition, because the constructor is private, the Singleton class cannot be instantiated outside the class itself; therefore, the variable references the only instance that can exist in the system.

Because the Singleton instance is referenced by private static member variables, instantiation does not occur until the class is first referenced by a call to the Instance property. The only potential drawback of this approach is that you have less control over the instantiation mechanism. In Design Patterns form, you can use non-default constructors or perform other tasks before instantiating. Because the .NET Framework is responsible for initialization in this solution, you do not have these options. In most cases, static initialization is the preferred way to implement Singleton in .NET.

(2.5) delayed initialization:

(3) the advantages and disadvantages of singleton model:

(4) Application and application scenarios of singleton mode:

At this point, the study of "what is the .net singleton pattern" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report