Last active
March 20, 2021 04:58
-
-
Save sudhirkhanger/845b4b62ef51350f8ab84e66bb4fdaf1 to your computer and use it in GitHub Desktop.
How to initialize Singleton?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
companion object { | |
@Volatile | |
private var INSTANCE: WordRoomDatabase? = null | |
fun getDatabase(context: Context): WordRoomDatabase { | |
return INSTANCE ?: synchronized(this) { | |
val instance = Room.databaseBuilder(context.applicationContext, WordRoomDatabase::class.java, "word_database").build() | |
INSTANCE = instance | |
// return instance | |
instance | |
} | |
} | |
} | |
-------------------------------------------------------- | |
private lateinit var INSTANCE: TitleDatabase | |
fun getDatabase(any_param_you_might_need): return_type { | |
synchronized(TitleDatabase::class or this) { | |
if (!::INSTANCE.isInitialized) { | |
INSTANCE = initialize_here | |
} | |
} | |
return INSTANCE | |
} | |
---------------------------------------------------- | |
public class Solution { | |
private volatile static Solution uniqueInstance; | |
private Solution() { | |
} | |
@Override | |
public String toString() { | |
return String.valueOf(solutionHashMap); | |
} | |
public static Solution getInstance() { | |
if (uniqueInstance == null) { | |
synchronized (Solution.class) { | |
if (uniqueInstance == null) { | |
uniqueInstance = new Solution(); | |
} | |
} | |
} | |
return uniqueInstance; | |
} | |
} | |
---------------------------------------------------------------------- | |
companion object { | |
private lateinit var sInstance: StockLiveData | |
@MainThread | |
fun get(symbol: String): StockLiveData { | |
sInstance = if (::sInstance.isInitialized) sInstance else StockLiveData(symbol) | |
return sInstance | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment