To: What is the difference between static class and Singleton?

Source: Internet
Author: User

Currently, the singleton mode is not fully implemented using static functions.

1. Ease of use: If Initialization is required, the singleton mode can be completed in the constructor. the class of all static functions requires an additional function to complete initialization.
If you call this initialize function, there will be problems in subsequent operations. The constructor will be called by default, so it is easy to use and has made the least assumptions for the user.

2. Initialization time: the singleton mode is flexible in initialization and can be initialized as needed. Full static functions will inevitably lead to all static members, and static members will be initialized at compilation. If
Initialization is expensive andProgramThis class may not be used in it, which will be a great advantage of the singleton mode. By the way, the initialization sequence of global variables is not specified.

For example, the global variable int A; int B; does the compiler initialize a first or B first? I think you can only guess, or experiment with a compiler to give the answer. What will happen if there is a new compiler?

3. The most important difference is that the singleton mode can be polymorphism, while the fully static classes cannot support polymorphism.

Currently, the singleton mode is not fully implemented using static functions.

1. Ease of use: If Initialization is required, the singleton mode can be completed in the constructor. the class of all static functions requires an additional function to complete initialization.
If you call this initialize function, there will be problems in subsequent operations. The constructor will be called by default, so it is easy to use and has made the least assumptions for the user.

2. Initialization time: the singleton mode is flexible in initialization and can be initialized as needed. Full static functions will inevitably lead to all static members, and static members will be initialized at compilation. If
Initialization costs are relatively expensive, and the class may not be used in the program, which is a great advantage of the singleton mode. By the way, the initialization sequence of global variables is not specified.

For example, the global variable int A; int B; does the compiler initialize a first or B first? I think you can only guess, or experiment with a compiler to give the answer. What will happen if there is a new compiler?

3. The most important difference is that the singleton mode can be polymorphism, while the fully static classes cannot support polymorphism.

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.