设计模式——状态模式

2020-11-02  本文已影响0人  DevilRoshan

什么是状态模式?

当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

实现

type State interface {
    On(machine *Machine)
    Off(machine *Machine)
}

type Machine struct {
    current State
}

func NewMachine() *Machine {
    return &Machine{current:NewOff()}
}

func (this *Machine)SetCurrent(state State) {
    this.current = state
}

func (this *Machine)On(){
    this.current.On(this)
}

func (this *Machine)Off(){
    this.current.Off(this)
}

type On struct {
}

func NewOn() State {
    return &On{}
}

func (this *On)On(machine *Machine){
    fmt.Println("设备已经开启")
}

func (this *On)Off(machine *Machine){
    fmt.Println("从On到Off状态,关闭")
    machine.SetCurrent(NewOff())
}

type Off struct {
}

func NewOff() State {
    return &Off{}
}

func (this *Off)On(machine *Machine){
    fmt.Println("从Off状态开启至On状态")
    machine.SetCurrent(NewOn())
}

func (this *Off)Off(machine *Machine){
    fmt.Println("设备已经关闭")
}
func TestState(t *testing.T) {
    machine := NewMachine()
    machine.Off()
    machine.On()
    machine.On()
    machine.On()
    machine.Off()
    machine.Off()
    machine.On()
}
// === RUN   TestState
// 设备已经关闭
// 从Off状态开启至On状态
// 设备已经开启
// 设备已经开启
// 从On到Off状态,关闭
// 设备已经关闭
// 从Off状态开启至On状态
// --- PASS: TestState (0.00s)
// PASS

优点

缺点

使用场景

注意

上一篇 下一篇

猜你喜欢

热点阅读