对象行为型模式——状态模式(StatePattern)

  • 定义:
    允许一个对象在其内部状态改变时改变其行为
  • 概述
    状态模式和策略模式很相似,要注意区分其区别,也有三个(至少三个)比较重要的类
    状态类接口(State)
    状态模式首先定义一个状态类接口(State),声明其所有的动作
    具体状态类(SoldState、SoldOutState、NoQuarterState、HasQuarterState)
    每一种状态类都实现State,每种状态对应实现其方法
    环境类(GumballMachine)
    维护一个State类

  • 以下是状态图
    对象行为型模式——状态模式(StatePattern)_第1张图片

  • 实例(Kotlin语言)
    以下是状态类接口(State)

interface State {
    fun insertQuarter()
    fun ejectQuarter()
    fun turnCrank()
    fun dispense()
}

以下是接口的具体实现

//售出状态
class SoldState : State {
    private val gumballMachine: GumballMachine

    constructor(gumballMachine: GumballMachine) {
        this.gumballMachine = gumballMachine
    }

    override fun insertQuarter() {
        println("Please wait ,gumball is on the way ...")
    }

    override fun ejectQuarter() {
        println("Sorry,you already turned the crank")
    }

    override fun turnCrank() {
        println("Turning twice doesn't get you another gumball")
    }

    override fun dispense() {
        gumballMachine.releaseBall()
        if (gumballMachine.count>0) {
            gumballMachine.setStated(gumballMachine.noQuarterState)
        }else{
            gumballMachine.setStated(gumballMachine.soldOutState)
        }
    }
}

//售罄状态
class SoldOutState :State {
    private val gumballMachine:GumballMachine

    constructor(gumballMachine: GumballMachine){
        this.gumballMachine = gumballMachine
    }
    override fun insertQuarter() {
        println("We will turn your money back,because the gumball is sold out !")
        gumballMachine.setStated(gumballMachine.hasQuarterState)
    }

    override fun ejectQuarter() {
        println("money turned back")
        gumballMachine.setStated(gumballMachine.noQuarterState)
    }

    override fun turnCrank() {
        println("you turned,but the gumball is sold out !")
    }

    override fun dispense() {
        println("the gumball is sold out ,so no gumball dispense...")
    }
}

//有25美分
class HasQuarterState : State {
   private val gumballMachine: GumballMachine

    constructor(gumballMachine: GumballMachine) {
        this.gumballMachine = gumballMachine
    }

    override fun insertQuarter() {
        println("you have inserted quarter,can't insert another")
    }

    override fun ejectQuarter() {
        println("Quarter returned")
        gumballMachine.setStated(gumballMachine.noQuarterState)
    }

    override fun turnCrank() {
        println("you turned ...")
        if (gumballMachine.count>0) {
            gumballMachine.setStated(gumballMachine.soldState)
        }else{
            gumballMachine.setStated(gumballMachine.soldOutState)
        }
    }

    override fun dispense() {
        println("No gumball dispensed")
    }
}

//无25美分
class NoQuarterState:State {
    private val gumballMachine:GumballMachine
    constructor(gumballMachine: GumballMachine){
        this.gumballMachine = gumballMachine
    }

    override fun insertQuarter() {
        println("You inserted a quarter")
        gumballMachine.setStated(gumballMachine.hasQuarterState)
    }

    override fun ejectQuarter() {
        println("You haven't inserted a quarter")
    }

    override fun turnCrank() {
        println("You turned,but there's no quarter")
    }

    override fun dispense() {
        println("You need to pay quarter first")
    }
}

以下是环境类(GumballMachine)

class GumballMachine {
    val soldState:State
    val soldOutState:State
    val noQuarterState:State
    val hasQuarterState:State
    var state:State
    var count = 0
    constructor(numberGumballs:Int){
        soldState = SoldState(this)
        soldOutState = SoldOutState(this)
        noQuarterState = NoQuarterState(this)
        hasQuarterState = HasQuarterState(this)
        state = soldOutState
        this.count = numberGumballs
        if (numberGumballs>0) {
            state = noQuarterState
        }
    }

    fun insertQuarter(): Unit {
        state.insertQuarter()
    }

    fun ejectQuarter(): Unit {
        state.ejectQuarter()
    }

    fun turnCrank(): Unit {
        state.turnCrank()
        state.dispense()
    }

    fun setStated(state: State): Unit {
        this.state = state
    }

    fun releaseBall(): Unit {
        println("A gumball comes rolling out the slot ...")
        if (count !=0) {
            count -= 1
        }
    }

}

以下是测试及测试结果

fun main(args: Array) {
    //状态模式:State Pattern
    val gumballMachine = GumballMachine(3)

    println(gumballMachine.count)

    gumballMachine.insertQuarter()
    gumballMachine.turnCrank()

    println(gumballMachine.count)

    gumballMachine.insertQuarter()
    gumballMachine.turnCrank()

    println(gumballMachine.count)

    gumballMachine.insertQuarter()
    gumballMachine.turnCrank()

    println(gumballMachine.count)

    gumballMachine.insertQuarter()
    gumballMachine.turnCrank()

    println(gumballMachine.count)
}

//结果
3
You inserted a quarter
you turned ...
A gumball comes rolling out the slot ...
2
You inserted a quarter
you turned ...
A gumball comes rolling out the slot ...
1
You inserted a quarter
you turned ...
A gumball comes rolling out the slot ...
0
We will turn your money back,because the gumball is sold out !
you turned ...
the gumball is sold out ,so no gumball dispense...
0

Process finished with exit code 0

以下是类图
对象行为型模式——状态模式(StatePattern)_第2张图片


如有错误,请留言更正,或进580725421群讨论,以免误导其他开发者!!!

你可能感兴趣的:(oo设计模式)