diff --git a/.gitignore b/.gitignore index 7bec898..696b755 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Project exclude paths -/.gradle/ \ No newline at end of file +/.gradle/ +/.idea/ diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..0e65cea --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/builder/Builder.kt b/src/main/kotlin/builder/Builder.kt new file mode 100644 index 0000000..8b43955 --- /dev/null +++ b/src/main/kotlin/builder/Builder.kt @@ -0,0 +1,52 @@ +package builder + + +data class Person( + val name: String, + val email: String, + val phone: Int, + val address: String +) + +class PersonBuilder { + private var name: String = "default name" + private var email: String = "default name" + private var phone: Int = 0 + private var address: String = "default address" + + fun setName(value: String): PersonBuilder { + name = value + return this + } + + fun setEmail(value: String): PersonBuilder { + email = value + return this + } + + fun setPhone(value: Int): PersonBuilder { + phone = value + return this + } + + fun setAddress(value: String): PersonBuilder { + address = value + return this + } + + fun build(): Person { + return Person(name, email, phone, address) + } + + +} + +fun main() { + val somePerson = + PersonBuilder().setName("some name").setEmail("some email").setPhone(1111111).setAddress("some address").build() + println(somePerson) + val defaultPerson = PersonBuilder().build() + println(defaultPerson) + + +} \ No newline at end of file diff --git a/src/main/kotlin/command/Command.kt b/src/main/kotlin/command/Command.kt new file mode 100644 index 0000000..629218a --- /dev/null +++ b/src/main/kotlin/command/Command.kt @@ -0,0 +1,29 @@ +package command +interface Command{ + fun execute() +} + +class ConcreteCommand(private val receiver: Receiver) : Command{ + override fun execute() { + receiver.action() + } +} + +class Receiver(){ + fun action(){ + println("получатель выполняет действия") + } +} + +class Initializer(private val command: Command){ + fun executeCommand(){ + command.execute() + } +} + +fun main() { + val receiver = Receiver() + val concreteCommand = ConcreteCommand(receiver) + val initializer = Initializer(concreteCommand) + initializer.executeCommand() +} diff --git a/src/main/kotlin/decorator/Decorator.kt b/src/main/kotlin/decorator/Decorator.kt new file mode 100644 index 0000000..a7a57cc --- /dev/null +++ b/src/main/kotlin/decorator/Decorator.kt @@ -0,0 +1,58 @@ +package decorator + +abstract class Lesson { + abstract val title: String + abstract val description: String + + abstract fun learn() + +} + +class EnglishLesson(override val title: String, override val description: String) : Lesson() { + override fun learn() { + println("изучение английского: $title - $description") + } +} + +class SpanishLesson(override val title: String, override val description: String) : Lesson() { + override fun learn() { + println("изучение испанского: $title - $description") + } +} + +class VocabularyLessonDecorator(private val lesson: Lesson) : Lesson() { + override val description: String + get() = lesson.description + override val title: String + get() = lesson.title + + override fun learn() { + lesson.learn() + println("новые слова") + } +} + +class GrammarLessonDecorator(private val lesson: Lesson) : Lesson() { + override val description: String + get() = lesson.description + override val title: String + get() = lesson.title + + override fun learn() { + lesson.learn() + println("новая грамматика") + } +} + + +fun main() { + val spanish = SpanishLesson("урок 1", "введение в испанский") + + val vocabularySpanish = VocabularyLessonDecorator(spanish) + vocabularySpanish.learn() + val vocabularyAndGrammarSpanish = GrammarLessonDecorator(vocabularySpanish) + + vocabularyAndGrammarSpanish.learn() + + +} \ No newline at end of file diff --git a/src/main/kotlin/decorator/SecondDecorator.kt b/src/main/kotlin/decorator/SecondDecorator.kt new file mode 100644 index 0000000..625a89a --- /dev/null +++ b/src/main/kotlin/decorator/SecondDecorator.kt @@ -0,0 +1,59 @@ +package decorator + +interface Pizza { + val price: Int + + fun getTotalPrice(): Int + fun getDescription(): String +} + +class BasicPizza : Pizza { + override val price: Int = 100 + + override fun getTotalPrice(): Int = price + override fun getDescription(): String = "Базовая пицца" +} + +abstract class PizzaDecorator(protected val pizza: Pizza) : Pizza { + abstract override val price: Int + override fun getTotalPrice(): Int = price + pizza.getTotalPrice() +} + +class Meat(pizza: Pizza) : PizzaDecorator(pizza){ + override val price: Int = 50 + override fun getDescription(): String = "${pizza.getDescription()} с мясом" +} + +class Cheese(pizza: Pizza) : PizzaDecorator(pizza){ + override val price: Int = 100 + override fun getDescription(): String = "${pizza.getDescription()} с сыром" +} + +class Vegetables(pizza: Pizza) : PizzaDecorator(pizza){ + override val price: Int = 150 + override fun getDescription(): String = "${pizza.getDescription()} с овощами" + override fun getTotalPrice(): Int = price + this.pizza.getTotalPrice() +} + +fun main(){ + + val badPizza = BasicPizza() + println("${badPizza.getDescription()} ${badPizza.getTotalPrice()}") + + val normalPizza = Meat(badPizza) + println("${normalPizza.getDescription()} ${normalPizza.getTotalPrice()}") + + val goodPizza = Cheese(normalPizza) + println("${goodPizza.getDescription()} ${goodPizza.getTotalPrice()}") + + val awesomePizza = Vegetables(goodPizza) + println("${awesomePizza.getDescription()} ${awesomePizza.getTotalPrice()}") + + val allIngredients = Meat(Cheese(Vegetables(pizza = BasicPizza()))) + println("${allIngredients.getDescription()} ${allIngredients.getTotalPrice()}") + + val veganPizza = Cheese(Vegetables(pizza = BasicPizza())) + println("${veganPizza.getDescription()} ${veganPizza.getTotalPrice()}") + + +} \ No newline at end of file diff --git a/src/main/kotlin/singleton/Singleton.kt b/src/main/kotlin/singleton/Singleton.kt new file mode 100644 index 0000000..d9935b9 --- /dev/null +++ b/src/main/kotlin/singleton/Singleton.kt @@ -0,0 +1,29 @@ +package singleton + +class GameConfig private constructor(){ + private var heroPull : Int = 124; + private var latency: Int = 500; + private var waitingPlayers: Int = 0; + private var matchName: String = "default" + + companion object { + val instance : GameConfig by lazy { GameConfig() } + } + + fun addPlayers(int: Int){ + this.waitingPlayers +=int + } + fun printConfig(){ + println("$matchName $heroPull $latency $waitingPlayers") + } +} + +fun main(){ + val config = GameConfig.instance + val anotherConfig = GameConfig.instance + anotherConfig.addPlayers(23) + config.addPlayers(23) + + config.printConfig() + +} \ No newline at end of file