This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InitialState: State{ | |
override fun parseCharacter(context: StateContext, character: Char){ | |
when{ | |
character == '+' || character == '-' -> { | |
context.setState(SignState()) | |
} | |
character == '0' -> { | |
context.setState(ZeroState()) | |
} | |
character in ('1'..'9') -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface State{ | |
fun parseCharacter(context: StateContext, character: Char) | |
val final: Boolean | |
} | |
class StateContext { | |
private var state: State? = null | |
fun setState(state: State?){ | |
this.state = state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(){ | |
var counter = 0 | |
val checkbox = CheckBox() | |
val printObserver = CheckboxObserver{ println("Checkbox have been clicked") } | |
var countObserver = CheckboxObserver{ counter++ } | |
// Nothing happens | |
checkbox.click() | |
checkbox.click() | |
checkbox.click() | |
checkbox.addObserver(countObserver) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheckBox(){ | |
var isChecked: Boolean = false | |
private val observers = mutableListOf<CheckboxObserver>() | |
fun addObserver(observer: CheckboxObserver){ | |
observers.add(observer) | |
} | |
fun removeObserver(observer: CheckboxObserver){ | |
observers.remove(observer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun interface CheckboxObserver{ | |
fun notifyClick() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheckBox(){ | |
var isChecked: Boolean = false | |
fun click(){ | |
isChecked = !isChecked | |
} | |
} | |
fun main(){ | |
val checkbox = CheckBox() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(){ | |
val frenchCheckBox = CheckBox() | |
val frenchTextView = TextView("Are you french?") | |
val mediator = FormMediator(frenchCheckBox, frenchTextView) | |
println("isChecked:${frenchCheckBox.isChecked} / text:${frenchTextView.getText()}") | |
frenchCheckBox.click() | |
println("isChecked:${frenchCheckBox.isChecked} / text:${frenchTextView.getText()}") | |
frenchTextView.click() | |
println("isChecked:${frenchCheckBox.isChecked} / text:${frenchTextView.getText()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FormMediator( | |
private val checkbox: CheckBox, | |
private val textview: TextView | |
): Mediator{ | |
init{ | |
checkbox.setMediator(this) | |
textview.setMediator(this) | |
} | |
override fun notifyCheckboxClicked(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheckBox(){ | |
var isChecked: Boolean = false | |
private var mediator: Mediator? = null | |
fun click(){ | |
isChecked = !isChecked | |
mediator?.notifyCheckboxClicked() | |
} | |
fun setMediator(mediator: Mediator){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Mediator{ | |
fun notifyCheckboxClicked() | |
fun notifyTextViewClicked() | |
} |