-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrategy.kt
35 lines (27 loc) · 728 Bytes
/
Strategy.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
package design_patterns
/**
* pattern: Strategy
*
* using: used when we need to change the behavior of an object
*
*/
interface ExchangeStrategy {
fun into(price: Double) : Double
class Dollar : ExchangeStrategy {
override fun into(price: Double): Double {
return price / 70
}
}
class Tenge : ExchangeStrategy {
override fun into(price: Double): Double {
return price * 6
}
}
}
class RubleExchangeRate {
private var strategy : ExchangeStrategy = ExchangeStrategy.Dollar()
fun changeStrategy(strategy: ExchangeStrategy) {
this.strategy = strategy
}
fun exchange(priceInRuble: Double) = strategy.into(priceInRuble)
}