-
Notifications
You must be signed in to change notification settings - Fork 109
/
Singleton.kt
39 lines (32 loc) · 900 Bytes
/
Singleton.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package design_patterns
/**
*
* Singleton is a generative design pattern that guarantees the existence of one instance of a class
*
* and provides a global access point to it
*
*/
object SQLiteDatabase {
// SQLiteDatabase instance state
private var connectionId = -1
// These methods provide a global access point to SQLiteDatabase instance state
fun openConnection() {
if (connectionId < 0) {
// open connection...
connectionId = 1
}
}
fun execSQL(sql: String): List<String> {
if (connectionId < 0) return emptyList()
return when (sql) {
"select * from names" -> listOf("Rick", "Morty", "Jerry", "Beth")
else -> emptyList()
}
}
fun closeConnection() {
if (connectionId > 0) {
// close connection...
connectionId = -1
}
}
}