kotlin状态模式
2018-11-19 本文已影响4人
腊鸭Laya
/**
* 状态模式
*/
interface State {
fun shopping()
fun move()
}
class Loving : State {
override fun move() {
println("一起看电影")
}
override fun shopping() {
println("一起看逛街")
}
}
class NoLove : State {
override fun move() {
println("不看电影")
}
override fun shopping() {
println("一个人逛街")
}
}
class Context2 {
private var mState: State? =null
private fun setState(state: State) {
mState = state
}
fun inLove() {
setState(Loving())
}
fun outLove() {
setState(NoLove())
}
fun move() {
mState!!.move()
}
fun shopping() {
mState!!.shopping()
}
}
fun main(args: Array) {
val context = Context2()
context.inLove()
context.move()
context.shopping()
context.outLove()
context.move()
context.shopping()
}