The first mock exam mode

1, The concept of singleton pattern

The so-called single instance is that the whole program has only one instance. This class is responsible for creating its own objects, while ensuring that only one object is created.

2, When to use singleton mode

Singleton pattern is one of the most used patterns in design pattern. You can use singleton pattern as long as you want to have only one object in the program.

3, How to use singleton mode

There are many scenarios in the single example mode, but the writing method is more sophisticated. As far as I know, there are five ways to write.

3.1 lazy type

// Starved Han model
// Thread is not safe, multiple instance objects may appear under multithreading
public class Singleton1 {

    private static Singleton1 instance;

    private Singleton1() {
    }

    public static Singleton1 getInstance() {
        if (instance == null) {
            instance = new Singleton1();
        }
        return instance;
    }

}

3.2 hungry Chinese style

// Hungry Han style
// Whether it is useful or not, the object will be created when the program starts. If the object is large, it will waste space.
public class Singleton2 {

    private static Singleton2 instance = new Singleton2();

    private Singleton2() {
    }

    public static Singleton2 getInstance() {
        return instance;
    }

}

3.3 double check lock

// Double search
// Solve the problem of lazy and hungry
// The writing method is more complicated
public class Singleton3 {

    private volatile static Singleton3 instance;

    public Singleton3() {
    }

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

}

3.4 static internal type

// Static inner class
// In this way, the inner class SingletonHolder is not used
// Then the jvm virtual machine will not load this class
public class Singleton4 {

    private static class SingletonHolder {
        private static final Singleton4 INSTANCE = new Singleton4();
    }

    private Singleton4() {
    }

    public static Singleton4 getInstance() {
        return SingletonHolder.INSTANCE;
    }

}

3.5 enumeration

// Enumerative
// The recommended writing method can deal with multiple serialization and reflection attacks
public enum Singleton5 {

    INSTANCE;

    public static Singleton5 getInstance() {
        return INSTANCE;
    }

}

4, Summary

The singleton mode definition and usage scenarios are very simple, just to ensure that there is only one object in memory. The following summarizes the advantages and disadvantages of several writing methods of single example:

No name advantage shortcoming
1 Slovenly simple Unsafe thread, deserialization attack and reflection attack
2 Hungry Han style simple There is no delay loading, which may waste space. There are deserialization attacks and reflection attacks
3 Double search Thread safety Complicated writing, deserialization attack and reflection attack
4 Static inner class Simpler than double search There are deserialization attacks and reflection attacks
5 Enumerative Simple, thread safe, can prevent deserialization attack and reflection attack /

The above is my understanding of the singleton model. Please point out the shortcomings. Thank you.

Tags: Programming jvm

Posted on Fri, 15 May 2020 11:23:10 -0400 by tomdelonge