我试图构建一个有限状态机来控制(iPhone SDK)Objective-C中的定时器。我认为这是必要的步骤,因为否则我会得到一堆充满if-then语句的混乱代码。复杂性、难以阅读和添加/更改功能的困难性促使我尝试像这样更正式的解决方案。
在应用程序的上下文中,计时器的状态确定了与NSManagedObjects、Core Data等的一些复杂交互。我现在把所有这些功能都留了下来,以便清楚地查看FSM代码。
问题在于,我找不到任何Obj-C中的这种代码示例,而且我对我从正在使用的C++示例代码翻译的正确性也不太自信。 (我根本不懂C++,所以需要一些猜测。)我基于这篇文章设计了一种状态模式,http://www.ai-junkie.com/architecture/state_driven/tut_state1.html。我没有做一个游戏,但这篇文章概述了我的工作所需的概念。
为了创建下面发布的代码,我不得不学习很多新概念,包括Obj-C协议等。由于这些都是对我来说比较新的概念,状态设计模式也是如此,我希望得到一些关于这个实现的反馈。这是否是你在Obj-C中有效使用协议对象的方式?
以下是协议:
@class Timer;
@protocol TimerState
-(void) enterTimerState:(Timer*)timer;
-(void) executeTimerState:(Timer*)timer;
-(void) exitTimerState:(Timer*)timer;
@end
这是计时器对象(最简化的形式)的头文件:
@interface Timer : NSObject
{
id<TimerState> currentTimerState;
NSTimer *secondTimer;
id <TimerViewDelegate> viewDelegate;
id<TimerState> setupState;
id<TimerState> runState;
id<TimerState> pauseState;
id<TimerState> resumeState;
id<TimerState> finishState;
}
@property (nonatomic, retain) id<TimerState> currentTimerState;
@property (nonatomic, retain) NSTimer *secondTimer;
@property (assign) id <TimerViewDelegate> viewDelegate;
@property (nonatomic, retain) id<TimerState> setupState;
@property (nonatomic, retain) id<TimerState> runState;
@property (nonatomic, retain) id<TimerState> pauseState;
@property (nonatomic, retain) id<TimerState> resumeState;
@property (nonatomic, retain) id<TimerState> finishState;
-(void)stopTimer;
-(void)changeState:(id<TimerState>) timerState;
-(void)executeState:(id<TimerState>) timerState;
-(void) setupTimer:(id<TimerState>) timerState;
计时器对象实现:
#import "Timer.h"
#import "TimerState.h"
#import "Setup_TS.h"
#import "Run_TS.h"
#import "Pause_TS.h"
#import "Resume_TS.h"
#import "Finish_TS.h"
@implementation Timer
@synthesize currentTimerState;
@synthesize viewDelegate;
@synthesize secondTimer;
@synthesize setupState, runState, pauseState, resumeState, finishState;
-(id)init
{
if (self = [super init])
{
id<TimerState> s = [[Setup_TS alloc] init];
self.setupState = s;
//[s release];
id<TimerState> r = [[Run_TS alloc] init];
self.runState = r;
//[r release];
id<TimerState> p = [[Pause_TS alloc] init];
self.pauseState = p;
//[p release];
id<TimerState> rs = [[Resume_TS alloc] init];
self.resumeState = rs;
//[rs release];
id<TimerState> f = [[Finish_TS alloc] init];
self.finishState = f;
//[f release];
}
return self;
}
-(void)changeState:(id<TimerState>) newState{
if (newState != nil)
{
[self.currentTimerState exitTimerState:self];
self.currentTimerState = newState;
[self.currentTimerState enterTimerState:self];
[self executeState:self.currentTimerState];
}
}
-(void)executeState:(id<TimerState>) timerState
{
[self.currentTimerState executeTimerState:self];
}
-(void) setupTimer:(id<TimerState>) timerState
{
if ([timerState isKindOfClass:[Run_TS class]])
{
secondTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(currentTime) userInfo:nil repeats:YES];
}
else if ([timerState isKindOfClass:[Resume_TS class]])
{
secondTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(currentTime) userInfo:nil repeats:YES];
}
}
-(void) stopTimer
{
[secondTimer invalidate];
}
-(void)currentTime
{
//This is just to see it working. Not formatted properly or anything.
NSString *text = [NSString stringWithFormat:@"%@", [NSDate date]];
if (self.viewDelegate != NULL && [self.viewDelegate respondsToSelector:@selector(updateLabel:)])
{
[self.viewDelegate updateLabel:text];
}
}
//TODO: releases here
- (void)dealloc
{
[super dealloc];
}
@end
不要担心这个类中缺少的东西,它目前还没有做任何有趣的事情。我目前只是在努力让语法正确。目前它能编译(并工作),但isKindOfClass方法调用会导致编译器警告(协议中找不到该方法)。我真的不确定是否要使用isKindOfClass。我想为每个
id<TimerState>
对象赋予一个名称字符串,并使用该名称字符串代替。
另外一点:所有那些id<TimerState>
声明最初都是TimerState *声明。将它们保留为属性似乎很有意义。不确定对于id<TimerState>
是否合适。
这是状态类之一的示例:#import "TimerState.h"
@interface Setup_TS : NSObject <TimerState>{
}
@end
#import "Setup_TS.h"
#import "Timer.h"
@implementation Setup_TS
-(void) enterTimerState:(Timer*)timer{
NSLog(@"SETUP: entering state");
}
-(void) executeTimerState:(Timer*)timer{
NSLog(@"SETUP: executing state");
}
-(void) exitTimerState:(Timer*)timer{
NSLog(@"SETUP: exiting state");
}
@end
目前为止,它除了宣布它处于哪个阶段(或子状态)之外,什么也没有做。但这不是重点。
我希望在这里学到的是这种架构是否在obj-c语言中正确组成。我遇到的一个具体问题是在定时器的init函数中创建id对象。如您所见,我注释掉了releases,因为它们导致“在协议中找不到释放”的警告。我不确定如何处理这个问题。
我不需要关于这段代码过度复杂或无意义形式主义的评论,或者其他任何东西。即使这些想法是真实的,我还是值得学习这个。如果有帮助,请把它看作是一种在obj-c中进行FSM的理论设计。
非常感谢您提供任何有用的评论。
(这篇文章对我没有太大帮助:Objective-C中的有限状态机)