Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/main/kotlin/ru/otus/homework/Builder.kt
Original file line number Diff line number Diff line change
@@ -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()
}
121 changes: 121 additions & 0 deletions src/main/kotlin/ru/otus/homework/Command.kt
Original file line number Diff line number Diff line change
@@ -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<Command> = 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()
}
40 changes: 40 additions & 0 deletions src/main/kotlin/ru/otus/homework/Decorator.kt
Original file line number Diff line number Diff line change
@@ -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()
}
17 changes: 17 additions & 0 deletions src/main/kotlin/ru/otus/homework/Singletone.kt
Original file line number Diff line number Diff line change
@@ -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"
}