. Net Singleton Mode (Singleton)

Source: Internet
Author: User

Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several fax cards, but there should only be one software that manages the fax card to avoid the case of two fax jobs being uploaded 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 a communication port from being called simultaneously by two requests.

Problem Description:

Singleton pattern Singleton Pattern

Problem solving:

(1) Introduction to the Singleton mode:

The singleton pattern requires that a class have only one instance and provide a global access point. This raises the question of how to bypass the regular constructors and provide a mechanism to guarantee that there is only one instance of a class. The client program, when invoking a class, does not consider whether the class can have only one instance, so it should be the responsibility of the Class Designer, not the class user.

Characteristics of the single-case mode:

(1.1) A class has and has only one instance

(1.2) class provides a global access point

(2) The implementation of the Singleton mode:

(2.1) Simple implementation:

Advantages of the above implementations:

(1) The initialization of the instance is not performed until the object requires it, which is known as lazy instantiation, which avoids creating unnecessary singleton when the program starts.

Disadvantages of the above implementations:

(1) This implementation is not safe for multithreaded environments, may instantiate multiple objects, for example, there may be two threads for instance==null judgment, and then create two singleton objects, which violates the design intent of the singleton pattern.

(2.2) Secure thread:

The above implementation guarantees that the Singleton object will only create one in the case of multithreading, but padlock adds additional overhead

(2.3) Double lock

This implementation is safe for multithreading, and the thread is not locked every time, only the judge object instance is not created when it is locked, with our first part of the analysis inside, we know that the lock after the object has been created after the judgment. It solves the problem of thread concurrency while avoiding exclusive locking in calls to get Instance. It also allows you to defer instantiation until the object is accessed for the first time. In fact, applications rarely require this type of implementation. In most cases we will use static initialization. There are still many drawbacks to this approach: lazy initialization cannot be implemented.

(2.4) Static initialization

In this implementation, an instance is created at the first reference to any member of the class, which is marked as sealed to prevent derivation from occurring, and the derivation may increase the instance. In addition, the variable is marked as ReadOnly, which means that the variable can be assigned only during static initialization (the example shown here) or in a class constructor. It can still be used to solve the two basic problems that Singleton mode tries to solve: global access and instantiation control. The public static property provides 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, so the variable refers to a unique instance that can exist in the system.

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

. Net Singleton Mode (Singleton)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.