Intent: • Ensurea class only has one instance, and provide a global point of access to it. Make sure that a class has only one instance and provides a global reference to obtain it. Applicability • theremust be exactly one instance of a class, and it must be accessible to clientsfrom a well-known access point. • There is only one instance, and the client needs to obtain it from a well-known access point. • Whenthe sole instance shocould be extensible bysubclassing, and clients shocould be able to use an extended instancewithout modifying their code. • When this unique instance should be extensible through subclass and the client should be able to use an extended instance without changing the code. Consequences • Controlled access to sole instance • control instance access • CED name space • reduce namespace • Permits refinement of operations and representation • allow refinement of operations and representations • Permits a variable number instances • instances with variable numbers • More flexible than class operations • More flexible UMLCode implementation than class operations. When Singleton is implicitly loaded, singleton is initialized. [Java]/*** ELE. Me *** @ author changsheng */public class Singleton {static private final Singleton singleton = new Singleton (); private Singleton () {} static public Singleton getInstance () {return singleton;} Use reflection to destroy Singleton [java] @ SuppressWarnings ("unchecked") public static void reflect () throws InstantiationException, IllegalAccessException, invocationTargetException {Class <Singleton> clazz = Single Ton. class;/* get constructors info */Constructor <?> [] Methods = (Constructor <Singleton> []) clazz. getDeclaredConstructors (); for (int I = 0; I <methods. length; I ++) {System. out. println (methods [I]);} methods [0]. setAccessible (true); Singleton s1 = (Singleton) methods [0]. newInstance (); Singleton s2 = (Singleton) methods [0]. newInstance (); System. out. println (s1 = s2);} lazy implementation. When Singleton2.getInstance is explicitly called, singleton2. [java]/*** lazy * @ author changsheng **/public class Singleton2 {static private Singleton2 singleton2; private Singleton2 () {} // The sychronized keyword is not mandatory and depends on your needs. Static synchronized public Singleton2 getInstance () {if (singleton2 = null) {singleton2 = new Singleton2 ();} return singleton2 ;}} lazy and ELE. Me are determined by the time consumed for single-instance instantiation. The dual-check mechanism mainly solves the efficiency of multi-thread concurrent access. For more information about Concurrency, see Java Concurrency in practice. [Java] class Singleton3 {static volatile private Singleton3 singleton3;/* why is the volatile keyword used here? Once the thread changes singleton3, other threads can immediately see */private Singleton3 () {}/ ***. Why do I need to use the dual-check mechanism? Avoid multiple thread queuing problems */static public Singleton3 getInstance () {if (singleton3 = null) {synchronized (Singleton3.class) {if (singleton3 = null) singleton3 = new Singleton3 () ;}return singleton3 ;}} destroys a singleton3 instance through serialization. [Java] public class Singleton3 implements Serializable {private static final long serialVersionUID = 1L; static private final Singleton3 s = new Singleton3 (); private Singleton3 () {} static public Singleton3 getInstance () {return s;} public static void main (String [] args) throws IOException, ClassNotFoundException {Singleton3 s1 = Singleton3.getInstance (); Singleton3 s2 = Singleton3.getInstance (); System. out. println (s1 = s2); ByteArrayOutputStream bos = new ByteArrayOutputStream (); ObjectOutputStream oos = new ObjectOutputStream (bos); oos. writeObject (s1); ByteArrayInputStream bis = new ByteArrayInputStream (bos. toByteArray (); ObjectInputStream ois = new ObjectInputStream (bis); Singleton3 s3 = (Singleton3) ois. readObject (); System. out. println (s1 = s3) ;}} the best way to implement Singleton in java. For details, refer to thinking in java [java] public enum Singleton {Instance; static public Singleton getInstance () {return Instance;} public int method1 () {return 1 ;} public int method2 () {return 2 ;}} can be expanded. [Java]/***** @ author changsheng */public class SingletonExtends {static private final SingletonExtends singleton = new SingletonExtends (); protected SingletonExtends () {} static public SingletonExtends getInstance () {return singleton;} public void method () {}} [java] public class SingletonExtendsEx extends SingletonExtends {static private final SingletonExtendsEx singleton = new SingletonEx TendsEx (); static public SingletonExtendsEx getInstance () {return singleton;} private SingletonExtendsEx () {super ();} public void method1 () {} public void method2 () {}} Note: when using a singleton instance, make sure that only one instance object is required. In javaWeb, when is the Servlet a single instance and multiple instances? Why is it a single instance? 1. Thread Security (of course, some conventions need to be observed when writing Servlet. For example, do not share instance variables in servlet. If so, concurrency control is required ). 2. Avoid excessive spam objects.