added pattern Command
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -8,7 +8,13 @@ package design_patterns
|
||||
* description: to create an object, an auxiliary class Builder is used, which has methods for changing the fields of our main class.
|
||||
*/
|
||||
|
||||
class Pony {
|
||||
|
||||
/**
|
||||
* first variant
|
||||
*
|
||||
*/
|
||||
|
||||
class Pony1 {
|
||||
private val name: String
|
||||
private val family: String
|
||||
private val cutieMark: String
|
||||
@ -48,8 +54,68 @@ class Pony {
|
||||
this.city = city
|
||||
}
|
||||
|
||||
fun build() = Pony(name, family, cutieMark, city)
|
||||
fun build() = Pony1(name, family, cutieMark, city)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* the second variant
|
||||
*/
|
||||
|
||||
class Pony2 {
|
||||
private var name: String = ""
|
||||
private var family: String = ""
|
||||
private var cutieMark: String = ""
|
||||
private var city: String = ""
|
||||
|
||||
fun name() = name
|
||||
fun family() = family
|
||||
fun cutieMark() = cutieMark
|
||||
fun city() = city
|
||||
|
||||
companion object {
|
||||
fun newBuilder() = Pony2().Builder()
|
||||
}
|
||||
|
||||
inner class Builder {
|
||||
|
||||
fun changeName(name: String) = apply {
|
||||
this@Pony2.name = name
|
||||
}
|
||||
|
||||
fun changeFamily(family: String) = apply {
|
||||
this@Pony2.family = family
|
||||
}
|
||||
|
||||
fun changeCutieMark(cutieMark: String) = apply {
|
||||
this@Pony2.cutieMark = cutieMark
|
||||
}
|
||||
|
||||
fun changeCity(city: String) = apply {
|
||||
this@Pony2.city = city
|
||||
}
|
||||
|
||||
fun build() = this@Pony2
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Kotlin variant with default arguments
|
||||
*/
|
||||
|
||||
class Pony3(
|
||||
private var name: String = "",
|
||||
private var family: String = "",
|
||||
private var cutieMark: String = "",
|
||||
private var city: String = ""
|
||||
) {
|
||||
fun name() = name
|
||||
fun family() = family
|
||||
fun cutieMark() = cutieMark
|
||||
fun city() = city
|
||||
}
|
||||
23
src/main/kotlin/design_patterns/Command.kt
Normal file
23
src/main/kotlin/design_patterns/Command.kt
Normal file
@ -0,0 +1,23 @@
|
||||
package design_patterns
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@ -11,8 +11,6 @@ class ReverseArray<T> {
|
||||
/**
|
||||
* creates and returns a reverse array
|
||||
*
|
||||
* @array - array
|
||||
* @return returns the reverse array
|
||||
*/
|
||||
fun compute(array: Array<T>) : Array<T> {
|
||||
val newArray = array.copyOf()
|
||||
|
||||
@ -3,7 +3,7 @@ package other
|
||||
/**
|
||||
* Algorithm for finding the square root of a number
|
||||
*
|
||||
* Wikipedia: https://shorturl.at/pvAQ7
|
||||
* Wikipedia: https://ru.wikipedia.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%BD%D0%B0%D1%85%D0%BE%D0%B6%D0%B4%D0%B5%D0%BD%D0%B8%D1%8F_%D0%BA%D0%BE%D1%80%D0%BD%D1%8F_n-%D0%BD%D0%BE%D0%B9_%D1%81%D1%82%D0%B5%D0%BF%D0%B5%D0%BD%D0%B8
|
||||
*/
|
||||
|
||||
class Sqrt {
|
||||
|
||||
@ -6,14 +6,14 @@ import org.junit.jupiter.api.Assertions.*
|
||||
internal class BuilderTest {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
fun test_first_variant() {
|
||||
|
||||
val name = "Twillight Sparkle"
|
||||
val cutieMark = "The magic star"
|
||||
val city = "Canterlot"
|
||||
val family = "Alicorn"
|
||||
|
||||
val pony = Pony.Builder().changeName(name)
|
||||
val pony = Pony1.Builder().changeName(name)
|
||||
.changeCutieMark(cutieMark)
|
||||
.changeCity(city)
|
||||
.changeFamily(family)
|
||||
@ -25,4 +25,41 @@ internal class BuilderTest {
|
||||
assertEquals(family, pony.family())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_the_second_variant() {
|
||||
|
||||
val name = "Twillight Sparkle"
|
||||
val cutieMark = "The magic star"
|
||||
val city = "Canterlot"
|
||||
val family = "Alicorn"
|
||||
|
||||
val pony = Pony2.newBuilder().changeName(name)
|
||||
.changeCutieMark(cutieMark)
|
||||
.changeCity(city)
|
||||
.changeFamily(family)
|
||||
.build()
|
||||
|
||||
assertEquals(name, pony.name())
|
||||
assertEquals(cutieMark, pony.cutieMark())
|
||||
assertEquals(city, pony.city())
|
||||
assertEquals(family, pony.family())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_third_variant() {
|
||||
|
||||
val name = "Twillight Sparkle"
|
||||
val city = "Canterlot"
|
||||
val family = "Alicorn"
|
||||
|
||||
val pony = Pony3(name, family, city = city)
|
||||
|
||||
assertEquals(name, pony.name())
|
||||
assertEquals("", pony.cutieMark())
|
||||
assertEquals(city, pony.city())
|
||||
assertEquals(family, pony.family())
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
60
src/test/kotlin/design_patterns/Command.kt
Normal file
60
src/test/kotlin/design_patterns/Command.kt
Normal file
@ -0,0 +1,60 @@
|
||||
package design_patterns
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions
|
||||
|
||||
internal class Command {
|
||||
|
||||
@Test
|
||||
fun test_1() {
|
||||
val commands = listOf(
|
||||
AddCommand(10),
|
||||
AddCommand(20),
|
||||
MultiCommand(2),
|
||||
MinusCommand(10)
|
||||
)
|
||||
|
||||
var actual = 0
|
||||
commands.forEach { command ->
|
||||
actual = command.execute(actual)
|
||||
}
|
||||
|
||||
Assertions.assertEquals(50, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_2() {
|
||||
val commands = listOf(
|
||||
MultiCommand(2),
|
||||
MultiCommand(2),
|
||||
MultiCommand(2),
|
||||
MultiCommand(2),
|
||||
MinusCommand(100),
|
||||
MultiCommand(-1)
|
||||
)
|
||||
|
||||
var actual = 1
|
||||
commands.forEach { command ->
|
||||
actual = command.execute(actual)
|
||||
}
|
||||
|
||||
Assertions.assertEquals(84, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun test_3() {
|
||||
val commands = listOf(
|
||||
AddCommand(-1),
|
||||
MinusCommand(1000),
|
||||
MultiCommand(-2)
|
||||
)
|
||||
|
||||
var actual = 1
|
||||
commands.forEach { command ->
|
||||
actual = command.execute(actual)
|
||||
}
|
||||
|
||||
Assertions.assertEquals(2000, actual)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user