Home  >  Article  >  Java  >  What is singleton pattern in java? What are the advantages?

What is singleton pattern in java? What are the advantages?

王林
王林forward
2019-11-25 15:00:243608browse

What is singleton pattern in java? What are the advantages?

1. What is the singleton pattern

The singleton pattern (Singleton Pattern) is one of the simplest design patterns in Java. The singleton pattern is: During the running of the program, some classes have at most one instance object. This pattern involves a single class that is responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its unique objects directly, without the need to instantiate an object of the class.

Recommended java related video tutorials: java course

2. Implementation ideas of singleton mode

1. Staticization Instance object, let the instance object and Class object bind to each other, and can be accessed directly through the Class class object;

2. Private construction method, prohibiting the creation of multiple instances through the construction method;

3. Provide a public static method to return the only instance of this class.

3. Advantages and Disadvantages of Singleton Mode

Advantages:

1. There is only one instance in the memory, which reduces the memory overhead, especially Instances are frequently created and destroyed.

2. Avoid multiple occupation of resources.

Disadvantages:

There is no interface and cannot be inherited, which conflicts with the single responsibility principle. A class should only care about the internal logic and not how to instantiate it outside.

Four. Several different ways of writing singleton mode

1. Lazy man style

package com.yx.singleton;
 
/**
 * Singleton
 * <p>初级原型-懒汉式</>
 * <p>优点:用到时再初始化</>
 * <p>缺点:并发下会产生多个实例</>
 * <p>不推荐</>
 *
 * @author yx
 * @date 2019/11/24 12:18
 */
public class Singleton {
 
    /**
     * 静态化实例对象
     */
    private static Singleton sInstance = null;
 
    /**
     * 私有构造方法
     */
    private Singleton() {
    }
 
    /**
     * 提供一个公共的静态方法, 用来返回这个类的唯一实例.
     *
     * @return 单例对象
     */
    public static Singleton getInstance() {
        if (sInstance == null) {
            sInstance = new Singleton();
        }
        return sInstance;
    }
}

The above way of writing, in a concurrent environment, will Multiple instances occur, thread unsafe.

Enumeration method

/**
 * EnumSingleton
 * 枚举类单例模式
 *
 * <p>优点:不需要考虑序列化的问题;不需要考虑反射的问题</>
 * <p>缺点:所有的属性都必须在创建时指定, 也就意味着不能延迟加载; 并且使用枚举时占用的内存比静态变量的2倍还多</>
 *
 * @author yx
 * @date 2019/11/24 15:09
 */
public enum EnumSingleton {
    INSTANCE
}

For more related articles, please visit: java introductory learning

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

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete