Design pattern--Singleton mode

Source: Internet
Author: User

The concept of a singleton pattern

In Gof's design pattern: The basics of reusable object-oriented software, this is said: Ensure that a class has only one instance and provides a global access point to access it.

1. Why use Singleton mode

    • 1. Use global variables throughout the program space to share resources;
    • 2. In large-scale systems, for performance considerations, it is necessary to save the creation time of objects and so on;
    • 3. Share the same resource or manipulate the same object across multiple threads;

2, now a single example of common steps

    • 1. Constructor private/protected
    • 2. Provide a global static method (global access point)
    • 3. Define a static pointer in the class, a static variable pointer to a variable of this class

3, common single case pattern classification

The different instance initialization time can be divided into: a Hungry man type single case and lazy type single case

    • 1. A hungry man type Single example: static initialization, it is the class one loaded on the instantiation of the object, need to pre-occupy the system resources;
    • 2. Lazy-type singleton: The first time you are quoted, you will instantiate yourself; there are multithreading problems.


Second, the common single case method

1, lazy model--classic

(1) Not suitable for creating single-piece objects in multi-threaded situations

(2) Not suitable for calling the destructor of a specific class at the end of the program


2. Lazy Mode – Classic – plus lock

Thread is not secure, what should I do? The most intuitive way: add lock. However, lock is required for each call to the getinstance () method, which can become a performance bottleneck. How to improve?


3. Lazy mode – Double check lock mode

1. Why should I be sentenced to two times?

A) for the first time, if the M_psingleton exists, it returns directly;

b) Psingleton=null and two threads call getinstance () at the same time, they can all pass the first time, and then because of the lock mechanism, only one thread enters and the other is queued; if there is no second, then two are likely to create an instance


4. A Hungry man mode

Class is loaded on the instantiated object, so the thread security is ensured, when the performance requirements are relatively high, you can use this method, so as to avoid frequent lock and unlock caused by the waste of resources.

The implementation of a hungry man is a hidden danger, because there is no explicit provision in C + + for the construction order of global objects. If there is a global object A constructor that references the pointer in the A Hungry man form above, there will be a problem if the above singleton is not constructed before the a constructor is constructed.


5. A hungry man mode-static variable

Because static initialization is initialized by the main thread in a single-threaded manner at the beginning of the program, that is, before entering the main function, the static initialization of the instance guarantees thread safety. When performance requirements are high, you can use this approach to avoid the resource waste caused by frequent lock and unlock. Note: class copies and class assignments are prohibited.


6. Other questions: Does the memory need to be released?

Because the lifetime is the same object as the main thread, the program exits and the system reclaims all resources, including those without delete. or implement a private Cgarbo in a singleton:

7. Comparison between single-case model and static method

1, the singleton can inherit the class, implements the interface, but the static class cannot;

2, Singleton can be delayed initialization, static class is generally initialized at the first load;

3, the Singleton class can be used for polymorphism without forcing the user to assume only a unique instance, but generally do not use this method, directly with the template to achieve a simple example of easy!

4. The singleton mode executes when the new object is stored in the heap, but the static method does not need, it does not depend on the object (no this pointer), but he also needs memory, stored in the static storage area;

5. Static method classes are loaded when code is compiled, objects produced in static methods are freed when static methods are executed, and static methods in the class are executed without instantiating the class where the static method resides. In the case of Singleton mode, the only instance that is generated is always in memory and will not be purged by GC (because static attribute variables are not purged by GC) unless the program exits;


Reference: Liar design mode

Design pattern--Singleton mode

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.