. Net Singleton)

Source: Internet
Author: User

Each computer can have several printers, but only one Printer Spooler can be used to prevent two print jobs from being output to the Printer at the same time. Each computer may have several HbA cards, but only one software should be responsible for managing the HBA card to avoid the situation where two fax jobs are simultaneously transferred to the HBA card. Each computer can have several communication ports. The system should centrally manage these communication ports to prevent a communication port from being simultaneously called by both requests.

Problem description:

Singleton Pattern in Singleton Mode

Solution:

(1) Singleton mode introduction:

The Singleton mode requires that a class has only one instance and provides a global access point. This raises a question: how to bypass the conventional constructor and provide a mechanism to ensure that a class has only one instance? When a customer program calls a class, it does not consider whether the class can only have one instance or other issues. Therefore, this should be the responsibility of the class designer, not the responsibility of the class user.

Singleton Mode features:

(1.1) one class has only one instance

(1.2) class provides a Global Access Point

(2) Implementation of Singleton mode:

(2.1) Simple implementation:

Advantages of the above implementation:

(1) instance Initialization is not performed until the object is required. This implementation method is called "inert instantiation", which avoids unnecessary Singleton during program startup.

Disadvantages of the above implementation:

(1) This implementation is not safe for the multi-threaded environment and may instantiate multiple objects. For example, there may be two threads to judge instance = null, and then create two Singleton objects, this violates the design intent of the singleton mode.

(2.2) Secure threads:

The above implementation ensures that only one single-instance object will be created in the case of multiple threads, but padlock adds additional overhead.

(2.3) double lock

This implementation method is safe for multithreading, And the thread does not lock every time. It is locked only when it determines that the object instance is not created. With the analysis in the first part of the above, we know that after locking, We have to judge whether the object has been created. It solves the thread concurrency problem and avoids exclusive locking in the call to obtain the Instance. It also allows you to delay Instantiation to the first time an object is accessed. In fact, applications seldom need this type of implementation. In most cases, static Initialization is used. This method still has many disadvantages: delayed initialization cannot be implemented.

(2.4) Static Initialization

In this implementation, an instance is created when any member of the class is referenced for the first time. This class is marked as sealed to prevent derivation, and the derivation may increase the instance. In addition, the variable is marked as readonly, which means that only variables can be allocated 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 mode tries to solve: Global Access and instantiation control. The public static attribute provides a global access point for the access instance. In addition, because constructors are private, the Singleton class cannot be instantiated outside the class itself; therefore, variables reference a unique instance that can exist in the system.

Because a Singleton Instance is referenced by a private static member variable, the class will not be instantiated until it is referenced by a call to the Instance attribute for the first time. The only potential disadvantage of this method is that you have less control over the instantiation mechanism. In the Design Patterns format, you can use non-Default constructors or execute other tasks before instantiation. Because. NET Framework is responsible for initialization in this solution, you do not have these options. In most cases, static Initialization is the preferred method to implement Singleton in. NET.

(2.5) Delayed initialization:

(3) Advantages and Disadvantages of Singleton mode:

(4) application scenarios and scenarios of the singleton mode:

(5) specific instance:

Summary:

The Singleton design pattern is a very useful mechanism for providing a single access point in an object-oriented application.

Related Article

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.