Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 1.21 KB

Day 007.md

File metadata and controls

42 lines (30 loc) · 1.21 KB

Singleton

Read about how to build a Singleton pattern in Java here:

How to make the perfect Singleton?

What the result code looks like:

public class SingletonClass implements Serializable {

    private static volatile SingletonClass sSoleInstance;

    //private constructor.
    private SingletonClass(){

        //Prevent form the reflection api.
        if (sSoleInstance != null){
            throw new RuntimeException("Use getInstance() method to get the single instance of this class.");
        }
    }

    public static SingletonClass getInstance() {
        if (sSoleInstance == null) { //if there is no instance available... create new one
            synchronized (SingletonClass.class) {
                if (sSoleInstance == null) sSoleInstance = new SingletonClass();
            }
        }

        return sSoleInstance;
    }

    //Make singleton from serialize and deserialize operation.
    protected SingletonClass readResolve() {
        return getInstance();
    }
}

Also read about Singleton in Flutter & Dart here:

Singletons in Flutter / Dart