Home >Java >JavaBase >what is java singleton pattern

what is java singleton pattern

王林
王林Original
2019-11-11 14:37:152768browse

what is java singleton pattern

What is the singleton pattern?

The singleton pattern is a common design pattern that ensures that instances of a class are singletons.

Advantages of singleton mode:

(1) First of all, it must save memory resources. No matter how frequently instances are created through exposed methods, they can be guaranteed to be created. The object is the same instance object in the system memory;

(2) Flexibility, since the creation of all instances is controlled by this class, all this class can flexibly change the instantiation process;

(3) Controlled access to instances. Singleton classes can easily control controlled access to unique instances;

Disadvantages of singleton mode:

(1) The singleton mode has no interface and is not easy to extend;

(2) You cannot use reflection mode to create a singleton, otherwise a new object will be instantiated;

(3) Use lazy singleton Pay attention to thread safety issues when using the example mode;

Ways to implement the singleton mode:

Singleton method

package index;
public class Superman {
    //空的构造器
    private Superman(){}
    
    //实例化对象
    private static Superman supermanInstance = new Superman();
    
    //获取实例化对象的静态函数getSupermanInstance()
    public static Superman getSupermanInstance(){
        return supermanInstance;
    }
}

The singleton is new in advance when the class is loaded, and a supermanInstance object is instantiated at the beginning. Whether your program needs to call this object or not, it is already ready; there is no delay. Loading, in order to reduce the program load, lazy loading is required in most cases, so this way of implementing a singleton is not the best choice.

Lazy singleton method (single-threaded writing)

package index;
public class Superman {
    //空的构造器
    private Superman(){}

    //实例化对象;只声明 不使用new进行实例化
    private static Superman supermanInstance = null;

    //获取实例化对象的静态函数getSupermanInstance()
    public static Superman getSupermanInstance(){
        if(supermanInstance==null){ //如果为null的情况再进行实例化
            supermanInstance = new Superman();
        }
        return  supermanInstance;
    }
}

Declare an object without instantiating it, and call the static factory when the program needs to instantiate it. Method getSupermanInstance(), the declared object is judged in the method, and if it is null, instantiate it. This writing method can achieve the effect of delayed loading, but it is not thread-safe. If there are two threads in multi-threading At the same time, calling the static factory method getSupermanInstance() may repeatedly create instances of this class, destroying the uniqueness of the singleton;

Recommended tutorial: Java tutorial

The above is the detailed content of what is java singleton pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn