kotlin状态模式

/**
* 状态模式
*/

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()

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容