Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39784403
Singleton mode (Singleton): A design pattern that is often used.
In Java applications. Singleton objects are guaranteed to be in one JVM. Only one instance of the object exists.
The main advantages are: 1, some classes are created more frequently, for some large objects, this is a very large system overhead.
2, eliminate the new operator, reduce the use of system memory frequency, reduce the GC pressure.
There are two main ways to realize the singleton mode: 1, lazy type. 2. A hungry man type
First, UML modeling
Second, the code implementation: 1, lazy type
/** * Demo Example: Singleton-the Singleton object can be guaranteed in one JVM. Only one instance of the object exists. * Disadvantage: This practice in multi-threaded environment, insecure * lazy */class Singleton {/** * holds a private static variable (also known as a class variable) to prevent the reference * * This assignment is null. The goal is to delay loading (because some analogies are large, so delay loading can help improve performance) */private static Singleton instance = null;/** Private construction method to prevent instantiation */private Singleton () {} /** static Factory method, create an instance-just create yourself here, and just create a */public static Singleton getinstance () {if (instance = = null) {instance = new Sin Gleton ();} return instance;} public void info () {System.out.println ("This is a test method ...");}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {Singleton S1 = Singleton. GetInstance ();/** * Call common Method */s1.info (); Singleton s2 = singleton.getinstance ();/** * Execution result is true. Description S1, s2 These two class variables all point to the same object in memory */system.out.println (S1 = = s2);}}
2. A Hungry man type
/** * A hungry man type */class Singleton {private static Singleton instance = new Singleton ();p rivate Singleton () {}public static Singl Eton getinstance () {return instance;} public void info () {System.out.println ("This is a test method ...");}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {Singleton S1 = Singleton. GetInstance ();/** * Call common Method */s1.info (); Singleton s2 = singleton.getinstance ();/** * Execution result is true, indicating S1, S2 these two class variables all point to the same object in memory */system.out.println (S1 = = s2);}}
3, assuming that the multi-threading, then the GetInstance () method to add synchronous synchronized, then a hungry man than the lazy style better. Although resource utilization is poor, it is not necessary to synchronize.
/** * * when considering multithreading. Here's how to do this:-Lazy * * Synchronize when creating classes. So just to separate the creation and getinstance (), and to create the Synchronizedkeyword * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *, the whole program only needs to create one instance, so performance will * * @author Leo */public class Singletontest {private static Singletontest instance = Null;private singletontest () {}p Rivate static synchronized void Syncinit () {if (instance = = null) {instance = new Singletontest ();}} public static Singletontest getinstance () {if (instance = = null) {Syncinit ();} return instance;}}
Iii. Summary
The singleton mode guarantees that a class has only one instance, and provides a way to access the global point, which is more flexible to guarantee the creation and interview constraints of the instance.
There is only one instance of the system. The construction method should therefore be private a hungry man: A static instance is created directly when the class is loaded. Lazy: The first time you need to create an instance, then the GetInstance method to add synchronous a hungry man style than the lazy style better. Although resource utilization is poor. But don't sync.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Java design mode steal run Series (vi) modeling and implementation of Singleton mode