Design mode-Singleton mode

Source: Internet
Author: User

Single Case design mode

Singleton is a pattern of creation in which a class takes Singleton mode, and when the class is created, it is possible to produce only one instance for external access and a global access point.

The core points of knowledge are as follows:

(1) The construction method of a class with a singleton design pattern is privatized (with private modification).

(2) The instantiation object of the class is generated within it and encapsulated as a private static type.

(3) Defines a static method that returns an instance of the class.

/**  * Method One * Single-instance mode implementation: A Hungry man, thread-safe but inefficient *  * public  class Singleton {  
Defines a private constructor method private Singleton () { }
Set the instance object itself to a property, plus the static and final modifiers for the private static final Singleton instance = new Singleton ();
The static method returns an instance of the class public static Singleton getinstancei () { return instance; } }

Method One is the legendary a hungry man pattern.
The advantages are: it is relatively simple to write, and there is no multithreading synchronization problem, which avoids the performance problem caused by synchronized;
The disadvantage is that when the class singleton is loaded, the static instance is initialized, the statically variable is created and the memory space is allocated, and since then the static instance object has occupied the memory (even if you have not yet used this instance). When a class is unloaded, the static variable is destroyed and the occupied memory is freed, so memory is consumed under certain conditions.

/**   * Method Two * Single-instance mode implementation: Full-Chinese, non-thread-safe * */Public  class Singleton {

Defines a private construction method (prevents instantiation via new Singleton ()) private Singleton () { }
Define a variable of type Singleton (uninitialized, note that the final keyword is not used here) private static Singleton instance;
Defines a static method (Singleton is initialized when called, but can cause repeated initialization problems when multithreaded access) public static Singleton getinstance () { if (instance = = NULL) instance = new Singleton (); return instance; }

Method Two is the legend of the full-Han mode
The advantage is: it is relatively simple to write, when the class singleton is loaded, static variable instance is not created and allocated memory space, when the GetInstance method is first called, initialize the instance variable, and allocate memory, Therefore, in some specific conditions will save memory;
The disadvantage is that multiple singleton instances are likely to occur in a concurrent environment.

/**   * Method Three * single-mode implementation: Full-Chinese, thread-safe simple implementation * */public  class Singleton {
Defines a private construction method (prevents instantiation via new Singleton ()) private Singleton () { }
Define a variable of type Singleton (uninitialized, note that the final keyword is not used here) private static Singleton instance;
Defines a static method (called to initialize the Singleton, using synchronized to avoid multi-threaded access, may cause a heavy re-initialization problem) public static synchronized Singleton GetInstance () { if (instance = = null) instance = new Singleton (); return instance; }

Method Three is simple optimization of method two
The advantage is that multiple singleton instances occur when using the Synchronized keyword to avoid multi-threaded access.
The disadvantage is that when synchronous methods are frequently called, the efficiency is slightly lower.

/**   * Method Four * single-mode optimal scheme * thread safety  and high efficiency   *   */  
Define a private construction method
} //define a static private variable (not initialized, not using the final keyword, using volatile to guarantee the visibility of the instance variable during multithreaded access, and avoid being called by another thread when the other variable attribute is not assigned when instance initialized) private static volatile Singleton instance;
Defines a common static method that returns an instance of the type
When an object is instantiated (without using a synchronous block of code, instance is not equal to NULL, returns the object directly, increasing the efficiency of the operation) if (instance = = null) {
Synchronizing code blocks (when an object is not initialized, use a synchronous block of code to guarantee that the object will not be created again after the first creation of the multi-threaded access) synchronized (singleton.class) {
Uninitialized, the initial instance variable if (instance = = null) { instance = new Singleton ();} } } return instance; } }

Method Four is the best implementation of singleton pattern. Memory footprint, high efficiency, thread safety, multi-threaded operation atomicity.

Design mode-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.