How to make a singleton for retrofit 2?

后端 未结 5 614
别跟我提以往
别跟我提以往 2021-01-20 18:40

If there exists multiple retrofit call, how can i make a singleton of a retrofit, so that there won\'t be repeated codes within the class, thereby get rid of unnecessary cod

5条回答
  •  被撕碎了的回忆
    2021-01-20 19:02

    public class Singleton {
    private volatile static Singleton singleton;
    private Singleton(){}
    public static Singleton getSingleton(){
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
    

提交回复
热议问题