设计模式系列(一)单例模式

    xiaoxiao2022-07-02  107

    单例模式,其实在android的源码中已经有现成的接口

    取自android-28

    package android.util; /** * Singleton helper class for lazily initialization. * * Modeled after frameworks/base/include/utils/Singleton.h * * @hide */ public abstract class Singleton<T> { private T mInstance; protected abstract T create(); public final T get() { synchronized (this) { if (mInstance == null) { mInstance = create(); } return mInstance; } } }

     

    最新回复(0)