Java implements singleton design pattern-creation type

Creation type

1. Singleton design pattern
2. Factory design pattern
3. Builder design pattern
4. Prototype design pattern

Structure type

5. Agency design pattern
6, bridging design pattern
7, decoration design pattern
8, adapter design pattern
9, appearance design pattern
10, flyweight design pattern
11, combination design pattern

Behavioral

12. Template design mode

Introduction

The Singleton Design Pattern is very simple to solve. A class is only allowed to create one object (or instance), then this class is a singleton class. This design pattern is called the singleton design pattern, or singleton pattern for short.

The general conditions to be considered when implementing a singleton:

1. The constructor is decorated with private, and the access authority is used to avoid external creation of instances (objects) through new;
2. Consider thread safety when creating objects;
3. Consider whether to support lazy loading;
4. Consider whether the performance of getInstance() is high (Whether to lock).

Method to realize

Singleton mode: hungry man, lazy man, double detection, static inner class, enumeration;

1. Hungry Chinese

public class Setting {
    private static final Setting mSetting= new Setting();

    private Setting() {
    }

    public static Setting getInstance() {
        return mSetting;
    }
}

Disadvantages: when the class is loaded, the static instance of mSetting has been created and initialized; lazy loading is not supported (create a singleton object when needed)

Advantages: the creation process of mSetting instance is thread-safe (during class loading, the static mSetting instance has been initialized, so the creation of mSetting instance is thread-safe)
2, lazy

public class Setting {
    private static Setting mSetting;

    private Setting() {
    }

    public static synchronized Setting getInstance() {
        if (mSetting == null) {
            mSetting = new Setting();
        }
        return mSetting;
    }
}

Disadvantages: getInstance() adds a lock. If it is called frequently, the efficiency is very low (lock and release each time), resulting in a low concurrency, which is 1;

Advantages: can be delayed loading;

3. Double detection

public class Setting {
    private static Setting mSetting;

    private Setting() {
    }

    public static  Setting getInstance() {
        if (mSetting == null) {
            synchronized (Setting.class) {
                if (mSetting == null) {
                    mSetting = new Setting();
                }
            }
        }
        return mSetting;
    }
}

Advantages: both lazy loading and high concurrency are supported

4. Static inner class

public class Setting {

    private Setting() {
    }

    private static class SettingHolder{
        private static final Setting mSetting = new Setting();
    }


    public static Setting getInstance() {
        return SettingHolder.mSetting;
    }
}

Advantages: while ensuring thread safety, lazy loading is also done; when the Setting class is loaded, the static internal class SettingHolder is not created, and it is loaded only when getInstance() is called; the uniqueness of mSetting, the thread safety of the creation process Performance is guaranteed by JVM;

5. Enumeration

public enum  Setting{
    INSTANCE
}

Summary: The above five implementations are all thread-safe;

Guess you like

Origin blog.csdn.net/ezconn/article/details/106507491