-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.kt
31 lines (26 loc) · 774 Bytes
/
Command.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
package design_patterns
/**
* pattern: Command
*
* description: it's a behavioral pattern that allows you to wrap requests or simple operations in separate objects.
*
* P.S. Kotlin variant of this pattern is shown in tests
*/
interface ArithmeticCommand {
fun execute(number: Int) : Int
}
class AddCommand(private val addendum: Int) : ArithmeticCommand {
override fun execute(number: Int): Int {
return number + addendum
}
}
class MinusCommand(private val subtrahend: Int) : ArithmeticCommand {
override fun execute(number: Int): Int {
return number - subtrahend
}
}
class MultiCommand(private val coefficient: Int) : ArithmeticCommand {
override fun execute(number: Int): Int {
return number * coefficient
}
}