A single embodiment mode implementations
Common
1, the way a hungry man
Sample code:
com.mt.singleton Package;
/ **
* singleton design pattern implementations of starving
* thread safe, efficient high call, but delay can not achieve loading
* @author the MT
*
* /
public class SingletonDemo1 {
Private static SingletonDemo1 S = SingletonDemo1 new new ();
// constructor privatization
Private SingletonDemo1 () { } // Add common method for acquiring an object, not delay loading public static SingletonDemo1 the getInstance () { return S; }
/ **
* singleton design pattern implementations of starving
* thread safe, efficient high call, but delay can not achieve loading
* @author the MT
*
* /
public class SingletonDemo1 {
Private static SingletonDemo1 S = SingletonDemo1 new new ();
// constructor privatization
Private SingletonDemo1 () { } // Add common method for acquiring an object, not delay loading public static SingletonDemo1 the getInstance () { return S; }
}
2, lazy way
Sample code:
com.mt.singleton Package;
/ **
* lazy singleton design pattern mode of implementation
* thread safe, lazy loading, but the call is not efficient
* @author the MT
*
* /
public class SingletonDemo2 {
Private static SingletonDemo2 S;
// privatization constructor
Private SingletonDemo2 () { } // Add common method for acquiring an object, the synchronization method call is low efficiency, retardation loading public static the synchronized SingletonDemo2 the getInstance () { IF (S == null) { S = new new SingletonDemo2 (); } return S; }
/ **
* lazy singleton design pattern mode of implementation
* thread safe, lazy loading, but the call is not efficient
* @author the MT
*
* /
public class SingletonDemo2 {
Private static SingletonDemo2 S;
// privatization constructor
Private SingletonDemo2 () { } // Add common method for acquiring an object, the synchronization method call is low efficiency, retardation loading public static the synchronized SingletonDemo2 the getInstance () { IF (S == null) { S = new new SingletonDemo2 (); } return S; }
}
3, static inner classes way
Sample code:
com.mt.singleton Package Penalty for;
/ **
* Design Pattern Singleton pattern of static inner class implementations
* thread-safe, high call efficiency, lazy loading
* @author MT
*
* /
public class SingletonDemo3 {
// static inner classes
static class InnerClass {Private Private static Final SingletonDemo3 new new SingletonDemo3 SC = (); } // constructor privatization Private SingletonDemo3 () { } // Add common method for acquiring an object, to achieve a delay loading public static SingletonDemo3 the getInstance () { return InnerClass .sc; }
/ **
* Design Pattern Singleton pattern of static inner class implementations
* thread-safe, high call efficiency, lazy loading
* @author MT
*
* /
public class SingletonDemo3 {
// static inner classes
static class InnerClass {Private Private static Final SingletonDemo3 new new SingletonDemo3 SC = (); } // constructor privatization Private SingletonDemo3 () { } // Add common method for acquiring an object, to achieve a delay loading public static SingletonDemo3 the getInstance () { return InnerClass .sc; }
}
4, enumeration implementations
Sample code:
com.mt.singleton Package Penalty for;
/ **
* Enumeration design pattern Singleton pattern of implementation
* thread-safe, high-efficiency calls, but does not implement lazy loading
* @author MT
*
* /
public enum SingletonDemo4 { // define a gold For the element, is itself a single enumeration embodiment INSTANCE; // add operation method public void instanceOperate () { }
/ **
* Enumeration design pattern Singleton pattern of implementation
* thread-safe, high-efficiency calls, but does not implement lazy loading
* @author MT
*
* /
public enum SingletonDemo4 { // define a gold For the element, is itself a single enumeration embodiment INSTANCE; // add operation method public void instanceOperate () { }
}
5, double-check the lock mode (rarely used, not recommended)
Preventing reflection and deserialization two single-mode embodiment
package com.mt.singleton;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.io.Serializable;
/ **
* singleton design pattern implementations of starving
* thread safe, efficient high call, but delay can not achieve loading
* @author the MT
*
* /
public class SingletonDemo6 the implements the Serializable {
Private static SingletonDemo6 new new SingletonDemo6 S = ();
// constructor privatization
Private SingletonDemo6 () {
IF (S = null!) {
the throw a RuntimeException new new ();
}
}
// Add common method for acquiring an object, not lazy loading
public static SingletonDemo6 the getInstance () {
return S;
}
Object readResolve public () throws the ObjectStreamExceptions was
{
return S;
}
* singleton design pattern implementations of starving
* thread safe, efficient high call, but delay can not achieve loading
* @author the MT
*
* /
public class SingletonDemo6 the implements the Serializable {
Private static SingletonDemo6 new new SingletonDemo6 S = ();
// constructor privatization
Private SingletonDemo6 () {
IF (S = null!) {
the throw a RuntimeException new new ();
}
}
// Add common method for acquiring an object, not lazy loading
public static SingletonDemo6 the getInstance () {
return S;
}
Object readResolve public () throws the ObjectStreamExceptions was
{
return S;
}
}
Authentication Code:
package com.mt.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
import com.mt.singleton.SingletonDemo6;
/ **
* singleton pattern (to prevent reflection and deserialized)
* @author the MT
*
* /
public class Client {
public static void main (String [] args) throws Exception {
SingletonDemo6 SingletonDemo6.getInstance S1 = ();
SingletonDemo6 S2 SingletonDemo6.getInstance = ();
System.out.println (S1);
System.out.println (S2);
/ * // preventing reflection
// get a class object
class <SingletonDemo6> clazz = (class <SingletonDemo6>) class. the forName ( "com.mt.singleton.SingletonDemo6");
// constructor object obtaining
the constructor <SingletonDemo6> C = clazz.getDeclaredConstructor (null);
// constructor privatization access
c.setAccessible (to true);
SingletonDemo6 S3 = C .newInstance ();
SingletonDemo6 c.newInstance S4 = ();
System.out.println(s3);
System.out.println(s4);*/
//防止反序列化
FileOutputStream os = new FileOutputStream("D:/a.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(s1);
oos.close();
os.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/a.txt"));
SingletonDemo6 s3 = (SingletonDemo6) ois.readObject();
System.out.println(s3);
}
* singleton pattern (to prevent reflection and deserialized)
* @author the MT
*
* /
public class Client {
public static void main (String [] args) throws Exception {
SingletonDemo6 SingletonDemo6.getInstance S1 = ();
SingletonDemo6 S2 SingletonDemo6.getInstance = ();
System.out.println (S1);
System.out.println (S2);
/ * // preventing reflection
// get a class object
class <SingletonDemo6> clazz = (class <SingletonDemo6>) class. the forName ( "com.mt.singleton.SingletonDemo6");
// constructor object obtaining
the constructor <SingletonDemo6> C = clazz.getDeclaredConstructor (null);
// constructor privatization access
c.setAccessible (to true);
SingletonDemo6 S3 = C .newInstance ();
SingletonDemo6 c.newInstance S4 = ();
System.out.println(s3);
System.out.println(s4);*/
//防止反序列化
FileOutputStream os = new FileOutputStream("D:/a.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(s1);
oos.close();
os.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/a.txt"));
SingletonDemo6 s3 = (SingletonDemo6) ois.readObject();
System.out.println(s3);
}
}
Summary: Several efficiency, lazy way slowest, because lazy to achieve a synchronization method, sometimes involved in the process of waiting, the other three are similar, relatively speaking.