The singleton pattern of common Java design patterns

First, the characteristics of the singleton pattern:

    1. The singleton pattern has one and only one instance;

    2. The singleton pattern must create its own unique instance;

    3. The singleton pattern must provide this instance to other objects; (provide a method to obtain the only instance externally)

Second, the advantages of the singleton pattern:

    Avoid repeated creation of instance objects, reduce time overhead when creating objects, and save memory space;

3. Common singleton pattern writing:

    1. Hungry Chinese

public class Singleton{
    private static Singleton st = new Singleton();
    private Singleton() {};
    public static Singleton getInstance() {
        return st;
    }    
}

    Hungry-style, as the name suggests, directly creates an instance every time it is used. The advantage is that there is no security problem of multi-threaded access.

    2. Lazy Man

public class Singleton{
    private static Singleton st = null;
    private Singleton() {};
    public static Singleton getInstance() {
        if(null == st) {
            st = new Singleton();
        }
        return st;
    }
}

    The advantage of lazy style over hungry style is that lazy loading is realized, and it is instantiated when it is specifically needed;

    The disadvantage is that the thread is unsafe and suitable for single-threaded operation;

    3. Lazy man style (thread safety, synchronization lock synchronized )

public class Singleton{
    private static Singleton st = null;
    private Singleton() {};
    public synchronized static Singleton getInstance() {
        if(null == st) {
            st = new Singleton();
        }
        return st;
    }
}

   Advantages: both lazy loading and thread safety are realized;

   Disadvantages: low performance, when the locking method is called, it is much slower than the general method call, so the performance loss of writing is high;

   4. Lazy man style (thread safety, double check lock)

public class Singleton {
        private static Singleton st = null;
	private Singleton() {
	};
	public static Singleton getInstance() {
		if (st == null) {
			synchronized (Singleton.class) {
				if (st == null) {
					st = new Singleton();
				}
			}
		}
		return st;
	}
}

    5. Static inner class

public class Singleton{  
    private static class SingletonHolder{  
        public static Singleton instance = new Singleton();  
    }  
    private Singleton(){}  
    public static Singleton getInstance(){  
        return SingletonHolder.instance;  
    }  
}  

    This method is to create an instance in the inner class, but because the inner class is private, the JVM will only create an instance object when the private inner class is called; and because it is written in the inner class, the class is used. Loading mechanism, there is no problem of multi-thread concurrency;    


    If you need to consider thread safety issues, it is recommended to use the 4th or 5th.

    The last thing I said to myself: the singleton pattern is a design idea. Many times we will create a new object at will, without considering whether to use a singleton to instantiate it. I think it is better than mastering several singletons. The more important thing about pattern writing is to consider performance issues at any time, and to apply the singleton pattern to the code more.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325475533&siteId=291194637