工作生活

通过构造器模式实现一个单例

2019-07-02  本文已影响0人  八百逗比奔北坡_f22e

通过构造器模式实现一个单例,仅供参考,不多说,直接看代码

import android.content.Context;
 
public class RxImageLoder {
 
    static RxImageLoder singleton;
 
    private RxImageLoder() {
    }
 
    public static RxImageLoder with(Context context) {
        if (singleton == null) {
            synchronized (RxImageLoder.class) {
                if (singleton == null) {
                    singleton = new Builder(context).build();
                }
            }
        }
        return singleton;
    }
 
    public static class Builder {
        private Context context;
 
        public Builder(Context context) {
            this.context = context;
        }
 
        public RxImageLoder build() {
            return new RxImageLoder();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读