The single case pattern is the simplest design pattern in the 23 design pattern, and it is also applied in the enterprise development particularly much. The advantage of Singleton mode is that there is only one instance in the project.
Features: constructor privatization, object privatization, only provides an external access interface.
Application Scenario:
1, the system needs to share resources: such as the log system, Spring resource manager, etc.
2, in order to control the use of resources: such as thread pool
Common applications in enterprise-level development and common frameworks:
Java EE in the Servlet,spring resource Manager (ie beans), database connection pool, thread pool, log system, website counters, etc.
Single-Case Pattern classification:
1, a Hungry man mode: A hungry man mode is the simplest code of the singleton mode, but the instance in the class initialization when the load, in the case of not immediately use, will slow the loading speed of the system, the specific code is as follows:
public class singleton{
private static Singleton instance = new Singleton ();
Private Singleton () {} public
static Singleton getinstance () {return
instance;
}
}
2, lazy mode: lazy mode compared to a hungry man mode, is in the instantiation of the only external interface to deal with the implementation of delayed loading, saving the system initialization time, but there is a thread unsafe situation.
public class singleton{
private static Singleton instance = null;
Private Singleton () {} public
static Singleton getinstance () {
if (instance = = null) {return
new Singleton ();
}
return instance
}
}
3, double check lock: Double check lock mode is in fact the lazy mode of upgrading, let the lazy mode become thread-safe. Note: There is a memory problem with the double check lock, which may invalidate the double check lock.
public class singleton{
private static Singleton instance = null;
Private Singleton () {} public
static Singleton getinstance () {
if (instance = = null) {
synchronized ( Singleton.class) {
if (instance = = null) {return
new Singleton ()
;
}}} return instance
}
}
4, Static internal class mode: Static internal class combines the lazy mode and the Evil Han mode of a bit: thread safety, delay loading.
public class singleton{
private static class singletonfactory{
private static Singleton INSTANCE = new Singleton ( );
}
Private Singleton () {} public
static Singleton getinstance () {return
singletonfactory.instance;
}
}
5, enumeration class pattern: should be the most perfect simple mode, not only thread security, but also to prevent the reverse sequence and reflection problems.
Enum singleton{
INSTANCE;
public void DoSomething () {
...}}
Single Case Mode detail problem:
1, Reflection break single case mode: Through reflection can destroy the implementation of the single case mode (except for enumeration class mode)
/**
* Using reflection to destroy a single case pattern
/public class Demo01 {public
static void Main (string[] args) throws Exception {
Singleton S1 = singleton.getinstance ();
Singleton s2 = singleton.getinstance ();
System.out.println (S1 = = s2);
Class<singleton> clazz = (class<singleton>) class.forname ("Com.singleton.Singleton");
Constructor<singleton> constructor = Clazz.getdeclaredconstructor (null);
Constructor.setaccessible (true);
Singleton s3 = Constructor.newinstance ();
System.out.println (S1 = = S3)
;
}
Class singleton{
private static Singleton instance = new Singleton ();
Private Singleton () {
///Prevent reflection from destroying simple mode, open comment section
//if (instance!= null) {
// throw new RuntimeException ();
The
public
static Singleton getinstance () {return
instance
}
}}
In fact, the so-called prevention means that it cannot be created by reflection.
2, deserialization break the single case mode (except enumerated class mode)
/**
* Deserialization break single case mode
*/public
class Demo02 {public
static void Main (string[] args) throws Exception {
S Ingleton S1 = singleton.getinstance ();
Singleton s2 = singleton.getinstance ();
System.out.println (S1 = = s2);
FileOutputStream fos = new FileOutputStream ("D://test.txt");
ObjectOutputStream oos = new ObjectOutputStream (FOS);
Oos.writeobject (S1);
Oos.close ();
Fos.close ();
ObjectInputStream ois = new ObjectInputStream (New FileInputStream ("D://test.txt"));
Singleton s3 = (Singleton) ois.readobject ();
System.out.println (S1 = = S3)
;
}
Class Singleton implements serializable{
private static Singleton instance = new Singleton ();
public static Singleton getinstance () {return
instance;
}
When deserializing, this method
//Private Object Readresolve () throws objectstreamexception{//return instance;< is invoked if the object already exists
c26/>//
//}
}
These two situations are limited to understanding and are not used much in the actual development process.
Thus, the single case pattern is complete.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.