-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractFactory.kt
73 lines (60 loc) · 1.56 KB
/
AbstractFactory.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package abstractfactory
interface Chat {
fun getChannels(): List<Channel>
fun getMessages(channelId: Long): List<Message>
}
data class Channel(
val id: Long,
val name: String,
val users: List<User>
)
data class Message(
val id: Long,
val text: String
)
sealed class User {
object Self : User()
class Other(val name: String) : User()
}
class SDKChatFactory : ChatFactory() {
override fun getChat() = TODO()
}
class FakeChatFactory : ChatFactory() {
override fun getChat() = FakeChat()
}
class FakeChat : Chat {
override fun getChannels(): List<Channel> {
return listOf(
Channel(
id = 1,
name = "First Channel",
users = listOf(User.Self, User.Other("Tom"))
)
)
}
override fun getMessages(channelId: Long): List<Message> {
return listOf(
Message(id = 1, text = "Hello"),
Message(id = 2, text = "How are you?")
)
}
}
class SDKChat : Chat {
override fun getChannels(): List<Channel> {
TODO("Not yet implemented")
}
override fun getMessages(channelId: Long): List<Message> {
TODO("Not yet implemented")
}
}
abstract class ChatFactory {
abstract fun getChat(): Chat
companion object {
inline fun <reified T : Chat> create(): ChatFactory =
when (T::class) {
FakeChat::class -> FakeChatFactory()
SDKChat::class -> SDKChatFactory()
else -> throw IllegalArgumentException()
}
}
}