In peacetime work, student learning and interview process, the singleton model as a common design pattern, will often be asked by the interviewer, even the written test will require students to write on the scene, the following will be a singleton mode of implementation of ideas and a few common ways to share simple.
Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that the class with the pattern can have only one instance of the class that is used in the system. That is, a class has only one instance of an object. is one of the most commonly used design patterns, friends familiar with the design patterns are not unfamiliar with the singleton model. General introduction of a single model of the book will refer to the A Hungry man and lazy type of the two implementation methods. But in addition to these two ways, this article also introduces several other ways to implement the singleton.
basic idea of realization
Singleton mode requires a class to have a reference to the return object ( always the same one ) and a method that obtains the instance (must be a static method, usually using the getinstance this name).
In layman's terms, a class is designed that only one object exists throughout the application (it is necessary to allow the outside of this class to create objects arbitrarily)
the implementation of the single example is mainly through the following three a step:
① Construction Method Privatization (temporarily irrespective of reflection)
② create good one objects internally and save them
③ provides a public static method that returns the address of an internal object
The first method of implementation:
a hungry man
type [
Static Constants
] the way
Example code:
Package Cn.itsource.sington;
/**
* a hungry man type[Static constant]
*
* @author Administrator
*
*/
Public class SingletonTest1 {
/**
* Singleton mode design a class so that only one instance of an object exists in this class
*
* 1 requires a private construction method to avoid externally created objects
*/
Private SingletonTest1 () {
}
/*
* 2 Create an object within this class and save it.
*/
Private Static SingletonTest1 Instance = new SingletonTest1 ();
/*
* 3 Publish a public static method to return the object that is stored internally
*
*/
Public Static SingletonTest1 getinstance () {
return instance;
}
}
Advantages: This is a simple way to do this, which is to instantiate the class when it is loaded. Thread synchronization issues are avoided.
Disadvantages: object creation is a class load, which can cause the class to load very slowly, did not reach the effect of Lazy Loading. If you have never used this instance from beginning to end, it can cause memory waste.
The second way of realization: A hungry man type [
Static code block
] the way
Example code:
Package Cn.itsource.sington;
Public class SINGLETONTEST3 {
Private SingletonTest3 () {
}
Private Static SingletonTest3 instance;
Static {
instance = new SingletonTest3 ();
}
Public Static SingletonTest3 getinstance () {
return instance;
}
}
This is similar to the way above, except that the process of instantiating a class is placed in a static block of code, and the code in the static code block is executed when the class is loaded, and the instance of the class is initialized. The pros and cons are the same as above.
The Third Way of realization: lazy-type [
double check
] the way
Example code:
Package Cn.itsource.sington;
/**
* Lazy Type
* @author Administrator
*/
Public class SingletonTest2 {
/**
* Requirements: Singleton mode design a class so that only one instance of an object exists in this class
*
* 1 requires a private construction method to avoid externally created objects
*/
Private SingletonTest2 () {
}
/*
* Class 2 is loaded into memory, the object does not exist, only when the getinstance () method is called, the
The elephant just started to create
*/
Private Static SingletonTest2 instance;
/*
* 3 Publish a public static method to return the object that is stored internally
* Lazy is lazy loading, if more than one thread at the same time to operate the lazy type when there is a risk of thread safety issues,
Troubleshooting Thread Safety issues:
can be synchronized to resolve. But after the synchronization, each time to compare the lock, the efficiency is slowed,
therefore, double judgment can be added to improve the efficiency of the program.
*/
Public Static SingletonTest2 getinstance () {
if (instance = = null) {
synchronized (SingletonTest2. class) {
if (instance = = null) {
instance = new SingletonTest2 ();
}
}
}
return instance;
}
}
double judgment, not unfamiliar to multithreaded developers, as shown in the code, we have two times if (instance = = null) check, so that the thread is safe. In this way, the instantiation code is executed only once, and when it is accessed again , the if (instance = = NULL)is determined, and the object is instantiated directly.
Advantages: Thread safety, lazy loading, high efficiency.
The difference between a hungry man and lazy-type:
1 A hungry man is a class one loaded into memory to create a good object;
2 Lazy Type the object does not exist when the class is loaded into memory , and only when the getinstance () method is called does the object start to create.
3 lazy is lazy load, if more than one thread to operate the lazy type when there is a risk of thread safety issues, to resolve the thread security issues can be synchronized to resolve. But after the synchronization, each time to compare the lock, efficiency becomes slow, so you can add double judgment to improve the efficiency of the program.
Fourth implementation: How to enumerate
Example code:
Package Cn.itsource.sington;
Public enum SingletonTest4 {
INSTANCE;
}
with an enumeration added in JDK1.5 to implement the singleton pattern. Not only can you avoid multithreading synchronization problems, but it also prevents deserialization from recreating new objects. It may be that enumerations are added in JDK1.5 , so it is rarely written in actual project development.
Advantages : It 's the biggest advantage. , followed by the ability to handle serialization by itself, is thread-safe
Disadvantages : when you want to instantiate a singleton class, you must remember to use the SingletonTest4. INSTANCE The way to get objects, instead of using new, can be confusing to other developers, especially when the source code is not visible.
the above is for a singleton mode several common ways to implement , in the teaching and learning process of a simple summary, I hope that you learn a single example of a little help.
Several implementations of Java single-instance mode