-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacade.kt
More file actions
53 lines (42 loc) · 1.03 KB
/
Facade.kt
File metadata and controls
53 lines (42 loc) · 1.03 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package design_patterns
/**
* pattern: Facade
*
* using: used to simplify access to an object with a complex implementation
*
* description: a complex object contains several dependencies within itself, which it combines with each other
*
*/
/**
* imitation of local data storage (database)
*
*/
class LocalDataSource {
private val data = mutableListOf<String>()
fun save(data: List<String>) {
this.data.addAll(data)
}
fun read() = data
fun isEmpty() = data.isEmpty()
}
/**
* network request simulation
*
*/
class NetworkDataSource {
fun get() = listOf(
"Harry Potter",
"Ronald Weasley",
"Hermione Granger"
)
}
class Repository(private val localSource: LocalDataSource, private val networkSource: NetworkDataSource) {
fun fetch() : List<String> {
// I omitted error handling for simplicity
if (localSource.isEmpty()) {
val data = networkSource.get()
localSource.save(data)
}
return localSource.read()
}
}