状态模式:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
优点:
1、状态模式:状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的判断逻辑转移表示不同状态的一系列类当中。
2、状态模式:将与特定状态相关的行为局部化,并且将不同状态的行为分割开来。消除庞大的条件分支语句。
缺点:
1、状态模式的使用必然会增加系统类和对象的个数。
2、状态模式的结构与实现都较为复杂,如果使用不当将导致程序结构和代码的混乱、降低代码可读性。
3、状态模式对“开闭原则”的支持并不太好,对于可以切换状态的状态模式,增加新的状态类需要修改那些负责状态转换的源代码,否则无法切换到新增状态;而且修改某个状态类的行为也需修改对应类的源代码。
总结:
1、状态模式允许一个对象基于内部状态而拥有不同的行为。
2、状态模式对“开闭原则”支持不是很好。
项目地址:https://github.com/GameRisker/Study-Design-Patterns.git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// // main.m // State // // Created by GameRisker on 2/21/16. // Copyright (c) 2016 GameRisker. All rights reserved. // #import "Work.h" #import <Foundation/Foundation.h> int main(int argc, const char *argv[]) { @autoreleasepool { // insert code here... Work *emergencyProjects = [[Work alloc] init]; emergencyProjects.m_hour = 9; [emergencyProjects writeProgram]; emergencyProjects.m_hour = 10; [emergencyProjects writeProgram]; emergencyProjects.m_hour = 12; [emergencyProjects writeProgram]; emergencyProjects.m_hour = 13; [emergencyProjects writeProgram]; emergencyProjects.m_hour = 14; [emergencyProjects writeProgram]; emergencyProjects.m_hour = 17; emergencyProjects.m_finished = false; [emergencyProjects writeProgram]; emergencyProjects.m_hour = 19; [emergencyProjects writeProgram]; emergencyProjects.m_hour = 22; [emergencyProjects writeProgram]; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// // Work.h // // // Created by GameRisker on 2/21/16. // // #import "State.h" #import <Foundation/Foundation.h> @interface Work : NSObject @property(strong, nonatomic) id<State> m_state; @property(nonatomic) CGFloat m_hour; @property(nonatomic) BOOL m_finished; - (void)writeProgram; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// // Work.m // // // Created by GameRisker on 2/21/16. // // #import "ForenoonState.h" #import "Work.h" @implementation Work - (instancetype)init { self = [super init]; if (self) { self.m_state = [[ForenoonState alloc] init]; } return self; } - (void)writeProgram { [self.m_state writeProgram:self]; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // SleepingState.h // // // Created by GameRisker on 2/21/16. // // #import "State.h" #import "Work.h" #import <Foundation/Foundation.h> @interface SleepingState : NSObject <State> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// // SleepingState.m // // // Created by GameRisker on 2/21/16. // // #import "SleepingState.h" @implementation SleepingState - (void)writeProgram:(Work *)work { NSLog(@"Sleeping State %f", work.m_hour); } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // EveningState.h // // // Created by GameRisker on 2/21/16. // // #import "State.h" #import <Foundation/Foundation.h> #import "Work.h" @interface EveningState : NSObject <State> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// // EveningState.m // // // Created by GameRisker on 2/21/16. // // #import "EveningState.h" #import "RestState.h" #import "SleepingState.h" @implementation EveningState - (void)writeProgram:(Work *)work { if (work.m_finished) { work.m_state = [[RestState alloc] init]; [work writeProgram]; } else { if (work.m_hour < 21) { NSLog(@"Evening State %f", work.m_hour); } else { work.m_state = [[SleepingState alloc] init]; [work writeProgram]; } } } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // RestState.h // // // Created by GameRisker on 2/21/16. // // #import "State.h" #import <Foundation/Foundation.h> #import "Work.h" @interface RestState : NSObject <State> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// // RestState.m // // // Created by GameRisker on 2/21/16. // // #import "RestState.h" @implementation RestState - (void)writeProgram:(Work *)work { NSLog(@"Rest State %f", work.m_hour); } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // NoonState.h // // // Created by GameRisker on 2/21/16. // // #import "State.h" #import <Foundation/Foundation.h> #import "Work.h" @interface NoonState : NSObject <State> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// // NoonState.m // // // Created by GameRisker on 2/21/16. // // #import "AfternoonState.h" #import "NoonState.h" @implementation NoonState - (void)writeProgram:(Work *)work { if (work.m_hour < 13) { NSLog(@"Noon State : %f", work.m_hour); } else { work.m_state = [[AfternoonState alloc] init]; [work writeProgram]; } } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // AfternoonState.h // // // Created by GameRisker on 2/21/16. // // #import "State.h" #import <Foundation/Foundation.h> #import "Work.h" @interface AfternoonState : NSObject <State> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// // AfternoonState.m // // // Created by GameRisker on 2/21/16. // // #import "AfternoonState.h" #import "EveningState.h" @implementation AfternoonState - (void)writeProgram:(Work *)work { if (work.m_hour < 17) { NSLog(@"After noon state %f ", work.m_hour); } else { work.m_state = [[EveningState alloc] init]; [work writeProgram]; } } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// // ForenoonState.h // // // Created by GameRisker on 2/21/16. // // #import "State.h" #import "Work.h" @interface ForenoonState : NSObject <State> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// // ForenoonState.m // // // Created by GameRisker on 2/21/16. // // #import "ForenoonState.h" #import "NoonState.h" @implementation ForenoonState - (void)writeProgram:(Work *)work { if (work.m_hour < 12) { NSLog(@"Forenoon state : %f", work.m_hour); } else { work.m_state = [[NoonState alloc] init]; [work writeProgram]; } } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // State.h // // // Created by GameRisker on 2/21/16. // // #import <Foundation/Foundation.h> @class Work; @protocol State <NSObject> - (void)writeProgram:(Work *)work; @end |