From 3f53a9262db95fb403ff562af6cda1f46edbb4b3 Mon Sep 17 00:00:00 2001 From: Ed_Krupenin Date: Tue, 25 Apr 2023 21:10:32 +0300 Subject: [PATCH] HomeWork2 fout pattern --- src/main/kotlin/ru/otus/homework/Builder.kt | 113 ++++++++++++++++ src/main/kotlin/ru/otus/homework/Command.kt | 121 ++++++++++++++++++ src/main/kotlin/ru/otus/homework/Decorator.kt | 40 ++++++ .../kotlin/ru/otus/homework/Singletone.kt | 17 +++ 4 files changed, 291 insertions(+) create mode 100644 src/main/kotlin/ru/otus/homework/Builder.kt create mode 100644 src/main/kotlin/ru/otus/homework/Command.kt create mode 100644 src/main/kotlin/ru/otus/homework/Decorator.kt create mode 100644 src/main/kotlin/ru/otus/homework/Singletone.kt diff --git a/src/main/kotlin/ru/otus/homework/Builder.kt b/src/main/kotlin/ru/otus/homework/Builder.kt new file mode 100644 index 0000000..9dce5f9 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Builder.kt @@ -0,0 +1,113 @@ +package src.main.kotlin.ru.otus.homework + +class House + +class Catalogue + +interface Builder { + fun reset() + fun buildDoor(door:String?) + fun buildWall(wall:String?) + fun buildStairs(stairs:String?) + fun buildWindow(window:String?) + fun buildRoof(roof:String?) +} + +class HouseBuilder() : Builder { + private lateinit var house:House + override fun reset() { + house = House() + println("build New House") + } + + override fun buildDoor(door:String?) { + println("build $door") + } + + override fun buildWall(wall:String?) { + println("build $wall") + } + + override fun buildStairs(stairs:String?) { + println("build $stairs") + } + + override fun buildWindow(window:String?) { + println("build $window") + } + + override fun buildRoof(roof:String?) { + println("build $roof") + } + + fun getResult() : House { + return house + } +} + +class CatalogueBuilder() : Builder { + private lateinit var catalogue:Catalogue + override fun reset() { + catalogue = Catalogue() + println("create New Catalog") + } + + override fun buildDoor(door:String?) { + println("add to catalog $door") + } + + override fun buildWall(wall:String?) { + println("add to catalog $wall") + } + + override fun buildStairs(stairs:String?) { + println("add to catalog $stairs ladder") + } + + override fun buildWindow(window:String?) { + println("add to catalog $window") + } + + override fun buildRoof(roof:String?) { + println("add to catalog $roof sloping tiled roof") + } + + fun getResult() : Catalogue { + return catalogue + } +} + +class Director() { + fun constructCottage(builder: Builder) { + builder.reset(); + builder.buildDoor("2 wood doors") + builder.buildWall("4 stone wall") + builder.buildStairs("ladder") + builder.buildWindow("6 small window") + builder.buildRoof("sloping tiled roof") + } + fun constructSkyscraper(builder: Builder) { + builder.reset(); + builder.buildDoor("12 12 automatic glass doors") + builder.buildWall("4 glass and concrete walls") + builder.buildStairs("5 escalators and two fire escapes") + builder.buildWindow("100 panoramic windows") + builder.buildRoof("Flat roof") + } +} + +fun main() { + val director = Director() + + var builder : Builder = HouseBuilder() + director.constructCottage(builder) + val house = (builder as HouseBuilder).getResult() + + builder = CatalogueBuilder() + director.constructCottage(builder) + val catalogue = builder.getResult() + + builder = HouseBuilder() + director.constructSkyscraper(builder) + val skyscraper = builder.getResult() +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/Command.kt b/src/main/kotlin/ru/otus/homework/Command.kt new file mode 100644 index 0000000..a811d02 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Command.kt @@ -0,0 +1,121 @@ +package ru.otus.homework +import java.util.Stack + +class Button() { + private lateinit var command : Unit + + fun click(){ + command + } + + fun setCommand(com: Unit) { + command = com + } +} + +class ShortCut() { + +} + +object MyApplication { + lateinit var clipboard: String + private lateinit var activeEditor: MyEditor + private val history: Stack = Stack() + val copyButton : Button = Button() + val cutButton : Button = Button() + val pasteButton : Button = Button() + val undoButton : Button = Button() + + fun createUI(){ + activeEditor = MyEditor() + activeEditor.text = "simple editor" + activeEditor.selection = "editor" + + commandExecute(CopyCommand(this, activeEditor)) + val copy = Unit + copyButton.setCommand(copy) + commandExecute(CutCommand(this, activeEditor)) + val cut = Unit + cutButton.setCommand(cut) + commandExecute(PasteCommand(this, activeEditor)) + val paste = Unit + pasteButton.setCommand(paste) + commandExecute(UndoCommand(this, activeEditor)) + val undo = Unit + undoButton.setCommand(undo) + } + + private fun commandExecute(command: Command){ + if (command.execute()) + history.push(command) + } + + fun undo() { + if (history.isEmpty()) + return + history.pop()?.undo() + } +} + +class MyEditor() { + fun deleteSelection() { + text.replace(selection,"") + } + + fun replaceSelection(clipboard: String) { + text.replace(selection,clipboard) + } + + lateinit var selection: String + lateinit var text: String +} + +abstract class Command(protected val app: MyApplication, protected val editor: MyEditor) { + lateinit var backup: String + + fun saveBackup() { + backup = editor.text + } + + fun undo() { + editor.text = backup + } + + abstract fun execute() : Boolean +} + +class CopyCommand(app: MyApplication, editor: MyEditor) : Command(app, editor) { + override fun execute(): Boolean { + app.clipboard = editor.selection + return false + } +} + +class CutCommand(app: MyApplication, editor: MyEditor) : Command(app, editor){ + override fun execute(): Boolean { + saveBackup() + app.clipboard = editor.selection + editor.deleteSelection() + return true + } +} + +class PasteCommand(app: MyApplication, editor: MyEditor) : Command(app, editor) { + override fun execute(): Boolean { + saveBackup() + editor.replaceSelection(app.clipboard) + return true + } +} + +class UndoCommand(app: MyApplication, editor: MyEditor) : Command(app, editor) { + override fun execute(): Boolean { + app.undo() + return false + } +} + +fun main(){ + MyApplication.createUI() + MyApplication.copyButton.click() +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/Decorator.kt b/src/main/kotlin/ru/otus/homework/Decorator.kt new file mode 100644 index 0000000..bf13711 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Decorator.kt @@ -0,0 +1,40 @@ +package src.main.kotlin.ru.otus.homework + +interface MyNotification { + fun myNotify() +} + +class MyMsgNotification(private val message:String?) : MyNotification { + override fun myNotify(){ + println(message) + } +} + +open class BasedMsgDecorator constructor (protected val myWrapper: MyNotification) : MyNotification { + override fun myNotify(){ + myWrapper.myNotify() + } +} + +class PhoneMsgDecorator constructor (myWrapper : MyNotification) : BasedMsgDecorator(myWrapper) { + override fun myNotify(){ + println("Notify by phone") + myWrapper.myNotify() + } +} + +class EmailMsgDecorator(myWrapper: MyNotification) : BasedMsgDecorator(myWrapper) { + override fun myNotify(){ + println("Notify by email") + myWrapper.myNotify() + } +} + +fun main() { + val myMessage = MyMsgNotification("Hello world") + // myMessage.myNotify() + val myMessageByPhone = PhoneMsgDecorator(myMessage) + // myMessageByPhone.myNotify() + val myMessageByEmail = EmailMsgDecorator(myMessageByPhone) + myMessageByEmail.myNotify() +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/Singletone.kt b/src/main/kotlin/ru/otus/homework/Singletone.kt new file mode 100644 index 0000000..b4ddb08 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/Singletone.kt @@ -0,0 +1,17 @@ +package ru.otus.homework + +// or +// object Singleton { +// fun doSomething() = "Doing something" +// } + +class Singleton private constructor() { + + companion object { + val instance: Singleton by lazy { + Singleton() + } + } + + fun doSomething() = "Doing something" +}