Java design Pattern--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 classSingletontest {
To define a private construction method
Privatesingletontest () {}
Set the instance object of itself to a property, plus the static and final modifiers
Private Static FinalSingletontest instance =Newsingletontest ();
Static methods return instances of this class
Public Staticsingletontest getinstancei () {returninstance; } }

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 singletontest 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 classSingletontest {

Define private construction methods (prevent instantiation with new Singletontest ())
Privatesingletontest () {}
Define a variable of type singletontest (uninitialized, note that the final keyword is not used here)
Private StaticSingletontest instance;
Defines a static method (Singletontest is initialized when called, but can cause repeated initialization problems when multithreaded access)
Public Staticsingletontest getinstance () {if(Instance = =NULL) Instance=Newsingletontest (); returninstance; } }

Method Two is the legend of the full-Han mode
The advantage is: it is relatively simple to write, when the class singletontest 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 singletontest instances are likely to occur in a concurrent environment.

/*** Method Three * Single-instance mode implementation: Full-Chinese, thread-safe simple implementation **/   Public classSingletontest {
//Define private construction methods (prevent instantiation with new Singletontest ()) Privatesingletontest () {}
//define a variable of type singletontest (uninitialized, note that the final keyword is not used here) Private StaticSingletontest instance;
//define a static method (re-initialize the singletontest when called and use synchronized to avoid multi-threaded access, which can cause heavy re-initialization problems) Public Static synchronizedsingletontest getinstance () {if(Instance = =NULL) Instance=Newsingletontest (); returninstance; } }

Method Three is simple optimization of method two
The advantage is that multiple singletontest 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 * Singleton mode optimal scheme * thread safe and high efficiency **/   Public classSingletontest {
Define a private construction method
PrivateSingletontest () {
}
//define a static private variable (uninitialized, do not use the final keyword, use volatile to guarantee the visibility of the instance variable during multithreaded access, avoid being called by another thread when the other variable property is not assigned when instance initialized) Private static volatileSingletontest instance;
Defines a common static method that returns an instance of the type
Public StaticSingletontest getistance () {
When an object is instantiated (without using a synchronous block of code, instance is not equal to NULL, the object is returned directly, improving operational efficiency)
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(Singletontest.class) {
Uninitialized, the initial instance variable
if(Instance = =NULL) {instance=Newsingletontest (); } } } returninstance; } }

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

Java 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.