@@ -8,42 +8,56 @@ package design_patterns
88 *
99 */
1010
11-
1211/* *
1312 * The first variant
1413 */
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
2021) {
2122
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()
2330
2431 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+ }
2941
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
3246 }
3347
34- fun changeFamily ( family : String ) = apply {
35- this .family = family
48+ fun connectTimeout ( timeout : Int ) = apply {
49+ connectTimeout = timeout
3650 }
3751
38- fun changeCutieMark ( cutieMark : String ) = apply {
39- this .cutieMark = cutieMark
52+ fun readTimeout ( timeout : Int ) = apply {
53+ readTimeout = timeout
4054 }
4155
42- fun changeCity ( city : String ) = apply {
43- this .city = city
56+ fun writeTimeout ( timeout : Int ) = apply {
57+ writeTimeout = timeout
4458 }
4559
46- fun build () = Pony1 (name, family, cutieMark, city )
60+ fun build () = HttpConnectionClient1 (dnsServerAddress, callTimeout, connectTimeout, readTimeout, writeTimeout )
4761
4862 }
4963
@@ -52,49 +66,69 @@ class Pony1 private constructor(
5266/* *
5367 * The second variant
5468 */
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()
6283
6384 companion object {
64- fun newBuilder () = Pony2 ().Builder ()
85+ fun newBuilder () = HttpConnectionClient2 ().Builder ()
6586 }
6687
6788 inner class Builder {
6889
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
7198 }
7299
73- fun changeFamily ( family : String ) = apply {
74- this @Pony2.family = family
100+ fun connectTimeout ( timeout : Int ) = apply {
101+ connectTimeout = timeout
75102 }
76103
77- fun changeCutieMark ( cutieMark : String ) = apply {
78- this @Pony2.cutieMark = cutieMark
104+ fun readTimeout ( timeout : Int ) = apply {
105+ readTimeout = timeout
79106 }
80107
81- fun changeCity ( city : String ) = apply {
82- this @Pony2.city = city
108+ fun writeTimeout ( timeout : Int ) = apply {
109+ writeTimeout = timeout
83110 }
84111
85- fun build () = this @Pony2
112+ fun build () = this @HttpConnectionClient2
86113 }
87114
88115}
89116
90117/* *
91118 * Kotlin variant with default arguments
92119 */
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
98126) {
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()
100134}
0 commit comments