在軟件開(kāi)發(fā)中,狀態(tài)機(jī)是一種強(qiáng)大的工具前塔,用于管理復(fù)雜系統(tǒng)中的狀態(tài)轉(zhuǎn)換缘眶。Objective-C開(kāi)發(fā)者現(xiàn)在可以通過(guò)TransitionKit這個(gè)輕量級(jí)庫(kù),優(yōu)雅地實(shí)現(xiàn)狀態(tài)機(jī)该抒。本文將介紹TransitionKit的主要特點(diǎn)顶燕、安裝方法、基本使用示例以及如何進(jìn)行單元測(cè)試欧引。
TransitionKit簡(jiǎn)介
TransitionKit是一個(gè)小巧的Cocoa庫(kù)芝此,提供了一個(gè)基于block的API因痛,用于在Objective-C中實(shí)現(xiàn)狀態(tài)機(jī)。它功能全面膊升,文檔完整谭企,并且經(jīng)過(guò)了徹底的單元測(cè)試评肆。狀態(tài)機(jī)是管理應(yīng)用程序復(fù)雜性的絕佳方式瓜挽,而TransitionKit提供了一個(gè)優(yōu)雅的API秸抚,讓你可以在iOS或Mac OS X應(yīng)用程序中實(shí)現(xiàn)狀態(tài)機(jī)歹垫。TransitionKit的主要特點(diǎn)
支持任意數(shù)量的狀態(tài)和事件:TransitionKit允許開(kāi)發(fā)者定義任意多的狀態(tài)和事件颠放。
基于block的回調(diào):狀態(tài)和事件支持一系列基于block的回調(diào),以便響應(yīng)狀態(tài)轉(zhuǎn)換暮芭。
遵守NSCopying和NSCoding協(xié)議:狀態(tài)、事件和狀態(tài)機(jī)都符合這些協(xié)議辕宏,便于在自定義類(lèi)中進(jìn)行歸檔和復(fù)制砾莱。
強(qiáng)類(lèi)型檢查:狀態(tài)機(jī)包含多個(gè)運(yùn)行時(shí)檢查,以確保配置正確聚假,便于調(diào)試和信任狀態(tài)機(jī)膘格。
支持用戶(hù)數(shù)據(jù):轉(zhuǎn)換支持通過(guò)userInfo字典包含任意用戶(hù)數(shù)據(jù)财松,便于在回調(diào)中廣播元數(shù)據(jù)。
文檔完備:整個(gè)庫(kù)都使用Appledoc進(jìn)行了標(biāo)記菜秦。
徹底的單元測(cè)試:確保庫(kù)的可靠性胚迫,讓你可以自信地進(jìn)行更改。
輕量級(jí):TransitionKit除了Foundation庫(kù)外沒(méi)有其他依賴(lài)褪尝,適用于iOS和Mac OS X。使用示例
以下是一個(gè)簡(jiǎn)單的狀態(tài)機(jī)示例河哑,模擬收件箱中消息的狀態(tài):
TKStateMachine *inboxStateMachine = [TKStateMachine new];
TKState *unread = [TKState stateWithName:@"Unread"];
[unread setDidEnterStateBlock:^(TKState *state, TKTransition *transition) {
[self incrementUnreadCount];
}];
TKState *read = [TKState stateWithName:@"Read"];
[read setDidExitStateBlock:^(TKState *state, TKTransition *transition) {
[self decrementUnreadCount];
}];
TKState *deleted = [TKState stateWithName:@"Deleted"];
[deleted setDidEnterStateBlock:^(TKState *state, TKTransition *transition) {
[self moveMessageToTrash];
}];
[inboxStateMachine addStates:@[unread, read, deleted]];
inboxStateMachine.initialState = unread;
TKEvent *viewMessage = [TKEvent eventWithName:@"View Message" transitioningFromStates:@[unread] toState:read];
TKEvent *deleteMessage = [TKEvent eventWithName:@"Delete Message" transitioningFromStates:@[read, unread] toState:deleted];
TKEvent *markAsUnread = [TKEvent eventWithName:@"Mark as Unread" transitioningFromStates:@[read, deleted] toState:unread];
[inboxStateMachine addEvents:@[viewMessage, deleteMessage, markAsUnread]];
// 激活狀態(tài)機(jī)
[inboxStateMachine activate];
[inboxStateMachine isInState:@"Unread"]; // YES, 初始狀態(tài)
// 觸發(fā)一些事件
NSDictionary *userInfo = nil;
NSError *error = nil;
BOOL success = [inboxStateMachine fireEvent:@"View Message" userInfo:userInfo error:&error]; // YES
success = [inboxStateMachine fireEvent:@"Delete Message" userInfo:userInfo error:&error]; // YES
success = [inboxStateMachine fireEvent:@"Mark as Unread" userInfo:userInfo error:&error]; // YES
success = [inboxStateMachine canFireEvent:@"Mark as Unread"]; // NO
// 錯(cuò)誤沙庐。不能將未讀消息標(biāo)記為未讀
success = [inboxStateMachine fireEvent:@"Mark as Unread" userInfo:nil error:&error]; // NO
// error 包含 TKInvalidTransitionError佳吞,帶有描述性錯(cuò)誤消息和失敗原因