forked from Suryakant-Bharti/Important-Java-Concepts
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Facade.kt
47 lines (34 loc) Β· 1.1 KB
/
Facade.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
40
41
42
43
44
45
46
47
import org.junit.jupiter.api.Test
class ComplexSystemStore(private val filePath: String) {
private val cache: HashMap<String, String>
init {
println("Reading data from file: $filePath")
cache = HashMap()
//read properties from file and put to cache
}
fun store(key: String, payload: String) {
cache[key] = payload
}
fun read(key: String): String = cache[key] ?: ""
fun commit() = println("Storing cached data: $cache to file: $filePath")
}
data class User(val login: String)
//Facade:
class UserRepository {
private val systemPreferences = ComplexSystemStore("/data/default.prefs")
fun save(user: User) {
systemPreferences.store("USER_KEY", user.login)
systemPreferences.commit()
}
fun findFirst(): User = User(systemPreferences.read("USER_KEY"))
}
class FacadeTest {
@Test
fun Facade() {
val userRepository = UserRepository()
val user = User("dbacinski")
userRepository.save(user)
val resultUser = userRepository.findFirst()
println("Found stored user: $resultUser")
}
}