in the Android source code you read, where have you seen the singleton pattern used in the Android framework layer or third-party frameworks? What is the way to implement the singleton pattern?
in the Android source code you read, where have you seen the singleton pattern used in the Android framework layer or third-party frameworks? What is the way to implement the singleton pattern?
EventBus,
/** Convenience singleton for apps using a process-wide EventBus instance. */
public static EventBus getDefault() {
EventBus instance = defaultInstance;
if (instance == null) {
synchronized (EventBus.class) {
instance = EventBus.defaultInstance;
if (instance == null) {
instance = EventBus.defaultInstance = new EventBus();
}
}
}
return instance;
}
android source code contains a lot of singleton patterns, such as the following implementation: static method with Synchronize lock
public static WindowManagerGlobal getInstance() {
synchronized (WindowManagerGlobal.class) {
if (sDefaultWindowManager == null) {
sDefaultWindowManager = new WindowManagerGlobal();
}
return sDefaultWindowManager;
}
}
too many, such as the commonly used LinkMovementMethod
public static MovementMethod getInstance() {
if (sInstance == null)
sInstance = new LinkMovementMethod();
return sInstance;
}