@@ -8,42 +8,56 @@ package design_patterns
8
8
*
9
9
*/
10
10
11
-
12
11
/* *
13
12
* The first variant
14
13
*/
15
- class Pony1 private constructor(
16
- private val name : String ,
17
- private val family : String ,
18
- private val cutieMark : String ,
19
- private val city : String
14
+ class HttpConnectionClient1 private constructor(
15
+ private val dnsServerAddress : String ,
16
+ private val callTimeout : Int ,
17
+ private val connectTimeout : Int ,
18
+ private val readTimeout : Int ,
19
+ private val writeTimeout : Int ,
20
+ // class can have many more fields
20
21
) {
21
22
22
- override fun toString () = " $name , $family , $cutieMark , $city "
23
+ override fun toString () = """
24
+ dns -> $dnsServerAddress
25
+ call timeout -> $callTimeout
26
+ connect timeout -> $connectTimeout
27
+ read timeout -> $readTimeout
28
+ write timeout -> $writeTimeout
29
+ """ .trimIndent()
23
30
24
31
class Builder {
25
- private var name: String = " "
26
- private var family: String = " "
27
- private var cutieMark: String = " "
28
- private var city: String = " "
32
+ private var dnsServerAddress: String = " 8.8.8.8"
33
+ private var callTimeout: Int = 0
34
+ private var connectTimeout: Int = 10_000
35
+ private var readTimeout: Int = 10_000
36
+ private var writeTimeout: Int = 0
37
+
38
+ fun dnsServerAddress (address : String ) = apply {
39
+ dnsServerAddress = address
40
+ }
29
41
30
- fun changeName (name : String ) = apply {
31
- this .name = name
42
+ fun callTimeout (timeout : Int ) = apply {
43
+ // we can add some checks such as:
44
+ // if (timeout < 0) throw IllegalArgumentException("Uncorrected timeout: $timeout")
45
+ callTimeout = timeout
32
46
}
33
47
34
- fun changeFamily ( family : String ) = apply {
35
- this .family = family
48
+ fun connectTimeout ( timeout : Int ) = apply {
49
+ connectTimeout = timeout
36
50
}
37
51
38
- fun changeCutieMark ( cutieMark : String ) = apply {
39
- this .cutieMark = cutieMark
52
+ fun readTimeout ( timeout : Int ) = apply {
53
+ readTimeout = timeout
40
54
}
41
55
42
- fun changeCity ( city : String ) = apply {
43
- this .city = city
56
+ fun writeTimeout ( timeout : Int ) = apply {
57
+ writeTimeout = timeout
44
58
}
45
59
46
- fun build () = Pony1 (name, family, cutieMark, city )
60
+ fun build () = HttpConnectionClient1 (dnsServerAddress, callTimeout, connectTimeout, readTimeout, writeTimeout )
47
61
48
62
}
49
63
@@ -52,49 +66,69 @@ class Pony1 private constructor(
52
66
/* *
53
67
* The second variant
54
68
*/
55
- class Pony2 {
56
- private var name: String = " "
57
- private var family: String = " "
58
- private var cutieMark: String = " "
59
- private var city: String = " "
60
-
61
- override fun toString () = " $name , $family , $cutieMark , $city "
69
+ class HttpConnectionClient2 private constructor() {
70
+ private var dnsServerAddress: String = " 8.8.8.8"
71
+ private var callTimeout: Int = 0
72
+ private var connectTimeout: Int = 10_000
73
+ private var readTimeout: Int = 10_000
74
+ private var writeTimeout: Int = 0
75
+
76
+ override fun toString () = """
77
+ dns -> $dnsServerAddress
78
+ call timeout -> $callTimeout
79
+ connect timeout -> $connectTimeout
80
+ read timeout -> $readTimeout
81
+ write timeout -> $writeTimeout
82
+ """ .trimIndent()
62
83
63
84
companion object {
64
- fun newBuilder () = Pony2 ().Builder ()
85
+ fun newBuilder () = HttpConnectionClient2 ().Builder ()
65
86
}
66
87
67
88
inner class Builder {
68
89
69
- fun changeName (name : String ) = apply {
70
- this @Pony2.name = name
90
+ fun dnsServerAddress (address : String ) = apply {
91
+ dnsServerAddress = address
92
+ }
93
+
94
+ fun callTimeout (timeout : Int ) = apply {
95
+ // we can add some checks such as:
96
+ // if (timeout < 0) throw IllegalArgumentException("Uncorrected timeout: $timeout")
97
+ callTimeout = timeout
71
98
}
72
99
73
- fun changeFamily ( family : String ) = apply {
74
- this @Pony2.family = family
100
+ fun connectTimeout ( timeout : Int ) = apply {
101
+ connectTimeout = timeout
75
102
}
76
103
77
- fun changeCutieMark ( cutieMark : String ) = apply {
78
- this @Pony2.cutieMark = cutieMark
104
+ fun readTimeout ( timeout : Int ) = apply {
105
+ readTimeout = timeout
79
106
}
80
107
81
- fun changeCity ( city : String ) = apply {
82
- this @Pony2.city = city
108
+ fun writeTimeout ( timeout : Int ) = apply {
109
+ writeTimeout = timeout
83
110
}
84
111
85
- fun build () = this @Pony2
112
+ fun build () = this @HttpConnectionClient2
86
113
}
87
114
88
115
}
89
116
90
117
/* *
91
118
* Kotlin variant with default arguments
92
119
*/
93
- class Pony3 (
94
- private var name : String = " " ,
95
- private var family : String = " " ,
96
- private var cutieMark : String = " " ,
97
- private var city : String = " "
120
+ class HttpConnectionClient3 (
121
+ private val dnsServerAddress : String = " 8.8.8.8" ,
122
+ private val callTimeout : Int = 0 ,
123
+ private val connectTimeout : Int = 10_000 ,
124
+ private val readTimeout : Int = 10_000 ,
125
+ private val writeTimeout : Int = 0
98
126
) {
99
- override fun toString () = " $name , $family , $cutieMark , $city "
127
+ override fun toString () = """
128
+ dns -> $dnsServerAddress
129
+ call timeout -> $callTimeout
130
+ connect timeout -> $connectTimeout
131
+ read timeout -> $readTimeout
132
+ write timeout -> $writeTimeout
133
+ """ .trimIndent()
100
134
}
0 commit comments