程序员

三十一、状态模式

2017-10-03  本文已影响0人  LeeLeCoder

1. 何为状态模式

状态模式允许一个对象在其内部状态改变的时候改变其行为,这个对象看上去就像是改变了它的类一样。

状态模式

环境(Context)角色(上下文):定义客户端所感兴趣的接口,并且保留一个具体状态类的实例.这个具体状态类的实例给出此环境对象的现有状态.

抽象状态(State)角色:定义一个接口,用以封装环境(Context)对象的一个特定的状态所对应的行为.

具体状态(ConcreteState)角色:每一个具体状态类都实现了环境(Context)的一个状态所对应的行为.

状态模式有两种处理行为:一种是所有行为在环境中进行处理,第二种在状态类通过持有环境(context)进行切换.

2. 代码演示

class Context {
    var state: State?
    
    func request(day: Int) {
        if day <= 1 {
            self.state = BuyState()
        } else if day <= 3 {
            self.state = SendState()
        } else {
            self.state = ReceiveState()
        }
        
        self.state?.handle()
    }
    
    func request2(day: Int) {
        self.state = BuyState()
        self.state?.innerHandle(day: day, context: self)
    }
}

class State {
    var context: Context?
    func handle() { }
    func innerHandle(day: Int, context: Context) { }
}

class BuyState: State {
    override func handle() {
        print("正在通知快递公司取件")
    }
    
    override func innerHandle(day: Int, context: Context) {
        var state: State = self
        
        if day <= 1 {
            self.context = context
        } else if (day <= 3) {
            state = SendState()
            state.context = context
        } else {
            state = ReceiveState()
            state.context = context
        }
        state.handle()
    }
}

class SendState: State {
    override func handle() {
        print("商品正在运输途中")
    }
}

class ReceiveState: State {
    override func handle() {
        print("已收货")
    }
}

let context: Context = Context()
context.request(day: 3)
context.request2(day: 5)

3. 优缺点

上一篇 下一篇

猜你喜欢

热点阅读