看下面的一个单例类:
public class AppCache {
private AppCache() {
}
private static class SingletonHolder {
private static AppCache sAppCache = new AppCache();
}
private static AppCache getInstance() {
return SingletonHolder.sAppCache;
}
public static void init(Application application) {
getInstance().onInit(application);
}
private void onInit(Application application) {
mContext = application.getApplicationContext();
ToastUtils.init(mContext);
Preferences.init(mContext);
ScreenUtils.init(mContext);
CrashHandler.getInstance().init();
CoverLoader.getInstance().init(mContext);
application.registerActivityLifecycleCallbacks(new ActivityLifecycle());
}
}
初看这段代码init方法里面,有调用getInstance().onInit(application);我感觉有点多此一举,并在群里和朋友讨论一番之后,我发现并非多此一举,如果这个单例我们是写给别人调用,这么写的好处就是,如果我们要改下getInstance().onInit的方法名