-
Notifications
You must be signed in to change notification settings - Fork 109
/
Builder.kt
134 lines (110 loc) · 3.57 KB
/
Builder.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package design_patterns
/**
*
* Builder is is a generative design pattern that is used to create complex objects,
*
* separates the construction of an object from its representation
*
*/
/**
* The first variant
*/
class HttpConnectionClient1 private constructor(
private val dnsServerAddress: String,
private val callTimeout: Int,
private val connectTimeout: Int,
private val readTimeout: Int,
private val writeTimeout: Int,
// class can have many more fields
) {
override fun toString() = """
dns -> $dnsServerAddress
call timeout -> $callTimeout
connect timeout -> $connectTimeout
read timeout -> $readTimeout
write timeout -> $writeTimeout
""".trimIndent()
class Builder {
private var dnsServerAddress: String = "8.8.8.8"
private var callTimeout: Int = 0
private var connectTimeout: Int = 10_000
private var readTimeout: Int = 10_000
private var writeTimeout: Int = 0
fun dnsServerAddress(address: String) = apply {
dnsServerAddress = address
}
fun callTimeout(timeout: Int) = apply {
// we can add some checks such as:
// if (timeout < 0) throw IllegalArgumentException("Uncorrected timeout: $timeout")
callTimeout = timeout
}
fun connectTimeout(timeout: Int) = apply {
connectTimeout = timeout
}
fun readTimeout(timeout: Int) = apply {
readTimeout = timeout
}
fun writeTimeout(timeout: Int) = apply {
writeTimeout = timeout
}
fun build() = HttpConnectionClient1(dnsServerAddress, callTimeout, connectTimeout, readTimeout, writeTimeout)
}
}
/**
* The second variant
*/
class HttpConnectionClient2 private constructor() {
private var dnsServerAddress: String = "8.8.8.8"
private var callTimeout: Int = 0
private var connectTimeout: Int = 10_000
private var readTimeout: Int = 10_000
private var writeTimeout: Int = 0
override fun toString() = """
dns -> $dnsServerAddress
call timeout -> $callTimeout
connect timeout -> $connectTimeout
read timeout -> $readTimeout
write timeout -> $writeTimeout
""".trimIndent()
companion object {
fun newBuilder() = HttpConnectionClient2().Builder()
}
inner class Builder {
fun dnsServerAddress(address: String) = apply {
dnsServerAddress = address
}
fun callTimeout(timeout: Int) = apply {
// we can add some checks such as:
// if (timeout < 0) throw IllegalArgumentException("Uncorrected timeout: $timeout")
callTimeout = timeout
}
fun connectTimeout(timeout: Int) = apply {
connectTimeout = timeout
}
fun readTimeout(timeout: Int) = apply {
readTimeout = timeout
}
fun writeTimeout(timeout: Int) = apply {
writeTimeout = timeout
}
fun build() = this@HttpConnectionClient2
}
}
/**
* Kotlin variant with default arguments
*/
class HttpConnectionClient3(
private val dnsServerAddress: String = "8.8.8.8",
private val callTimeout: Int = 0,
private val connectTimeout: Int = 10_000,
private val readTimeout: Int = 10_000,
private val writeTimeout: Int = 0
) {
override fun toString() = """
dns -> $dnsServerAddress
call timeout -> $callTimeout
connect timeout -> $connectTimeout
read timeout -> $readTimeout
write timeout -> $writeTimeout
""".trimIndent()
}