YBStatechart 1.0.2

YBStatechart 1.0.2

测试已测试
语言语言 Obj-CObjective C
许可证 Apache 2
发布最后发布2014 年 12 月

Ronald Mannak 维护。



  • Martijn The and Ronald Mannak

YBStatechart 是一个状态机框架。状态图是有限状态机的一种形式化类型,它源于 David Harel 在 1986 年对飞机系统软件架构设计的研究。他的白皮书文章值得一读。

状态图设计模式也用于 SproutCore 网络前端框架。他们写了一篇博客文章,比较了状态图和模型-视图-控制器模式。

设计实例

这是一个只有一个子状态的状态图的简单例子

YBState *rootState = [YBState stateWithName:@"rootState"];
YBState *loggedOut = [YBState stateWithName:@"loggedOut"];
YBState *loggedIn = [YBState stateWithName:@"loggedIn"];

[loggedIn onEnterState:^(YBState *_self) {
    // executed when loggedIn state is entered / activated
}];

[loggedIn onExitState:^(YBState *_self) {
    // executed when loggedIn state is exited / deactivated
}];

[loggedIn on:@"buttonPress" doBlock:^(YBState *_self) {
    // executed only when loggedIn is active
    // AND 
    // [statechart buttonPress] or [statechart dispatchEvent:@"buttonPress"] got called
}];

YBStatechart *statechart = [[YBStatechart alloc] init];
statechart.rootState = rootState;
[statechart activate];

可以通过在状态机上调用某个 -activateState...: 方法来激活状态,例如。

[statechart activateState:loggedIn];

现在,statechart 对象能够响应用户任意 Objective-C 消息,这些消息将被分派为“事件”发送到所有激活的子状态及其激活子状态,……例如

[statechart buttonPress];

这将调用与命名事件 buttonPress 相关联的块处理程序,但仅限于此刻激活的状态。

有关更多有用示例,请参阅David Harel 的白皮书

安装

CocoaPods 是安装 YBStatechart 的推荐方式

示例 podfile

pod 'YBLStatechart', '1.0.1'

许可证

YBLStatechart 在 Apache 软件许可证,2.0 ("Apache 2.0") 之下授权