iOS开发-状态模式

状态模式允许对象内部状态改变时改变它的行为,对象看起来好像修改了它的类。状态模式看起来和策略模式比较相像,策略模式是将可以互换的行为封装起来,然后通过使用委托的方式,决定使用哪一个行为,状态也是封装行为,不同的是可以将行为委托到当前状态。一个需要从外部设置,一个是内部通过状态变更达到行为变成的目的。

基础知识

状态模式的UML类图:

 

State封装基本的状态行为,我们通过Cotext上下文持有状态子类的实例,外部发起请求,我们就可以委托状态进行处理。地铁里面一般都有自动饮料售卖机,我们将所有的饮料都当做商品来看,我们如果需要买饮料需要两步,投币,取货,如果没有商品还可以退钱三种行为,关于自动售卖机的存在无钱状态,有钱状态,售卖中状态,已售罄状态四种状态,稍微思考一下,可以看下面的具体实现。

设计实战

状态行为基类: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@protocol StateProtocol <NSObject>
 
@optional
-(void)putMoney;
@optional
-(void)ejectMoney;
@optional
-(void)pressButton;
@optional
-(void)distribute;
 
@end
 
@interface State : NSObject<StateProtocol>
 
@end

 

售卖机处于无钱状态(NoMoneyState):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@protocol NoMoneyDelegate <NSObject>
 
-(void)setCurrentState:(State *)currentState;
 
-(State *)geHasMoneyState;
 
@end
 
//没钱状态
@interface NoMoneyState : State
 
@property (weak,nonatomic) id<NoMoneyDelegate>  delegate;
 
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@implementation NoMoneyState
 
-(void)putMoney{
    NSLog(@"NoMoneyState-putMoney:投放钱币");
    [self.delegate setCurrentState:[self.delegate geHasMoneyState]];
}
 
-(void)ejectMoney{
     NSLog(@"NoMoneyState-ejectMoney:没有投入钱币,无法退钱");
}
 
-(void)pressButton{
    NSLog(@"NoMoneyState-pressButton:请先投币");
}
 
-(void)distribute{
     NSLog(@"NoMoneyState-pressButton:请投币");
}
 
@end

售卖机处于有钱状态(HasMoneyState):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@protocol  HasMoneyDelegate <NSObject>
 
-(void)setCurrentState:(State *)currentState;
 
-(State *)getNoMoneyState;
 
-(State *)getSoldState;
 
@end
//有钱状态
@interface HasMoneyState : State
 
@property (weak,nonatomic) id<HasMoneyDelegate> delegate;
 
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@implementation HasMoneyState
 
-(void)putMoney{
    NSLog(@"HasMoneyState-putMoney:已经投入了钱,暂不支持投入");
}
 
-(void)ejectMoney{
    NSLog(@"HasMoneyState-ejectMoney:退钱,重新设置售卖机为无前状态");
    [self.delegate setCurrentState:[self.delegate getNoMoneyState]];
}
 
-(void)pressButton{
   NSLog(@"HasMoneyState-pressButton:按钮按下,取货");
   [self.delegate setCurrentState:[self.delegate getSoldState]];
}
 
-(void)distribute{
  NSLog(@"HasMoneyState-distribute:无法进行取出商品");
}
 
@end

售卖机处于售卖状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@protocol  SoldDelegate <NSObject>
 
-(void)setCurrentState:(State *)currentState;
 
-(void)realseProduct;
 
-(NSInteger)getCurrentCount;
 
-(State *)getNoMoneyState;
 
-(State *)getSoldOutState;
 
@end
//售出状态
@interface SoldState : State
 
@property (weak,nonatomic) id<SoldDelegate> delegate;
 
@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
@implementation SoldState
 
-(void)putMoney{
    NSLog(@"SoldState-putMoney:请稍后,正在进行商品出售");
}
 
-(void)ejectMoney{
     NSLog(@"SoldState-putMoney:请稍后,正在进行商品出售,无法退钱");
}
 
-(void)pressButton{
    NSLog(@"SoldState-putMoney:请在取出物品之后重新投币");
}
 
-(void)distribute{
    [self.delegate realseProduct];
    if ([self.delegate getCurrentCount]) {
        [self.delegate setCurrentState:[self.delegate getNoMoneyState]];
    }else{
        [self.delegate setCurrentState:[self.delegate getSoldOutState]];
    }
}
 
@end

售罄状态(SoldOutState):

1
2
3
4
5
6
7
8
9
10
11
@protocol  SoldOutDelegate <NSObject>
 
-(void)setCurrentState:(State *)currentState;
 
@end
//售罄状态
@interface SoldOutState : State
 
@property (weak,nonatomic) id<SoldOutDelegate>  delegate;
 
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@implementation SoldOutState
 
-(void)putMoney{
    NSLog(@"SoldOutState-PutMoney:已售罄");
}
 
-(void)ejectMoney{
    NSLog(@"SoldOutState-ejectMoney:无法退钱");
}
 
-(void)pressButton{
    NSLog(@"SoldOutState-pressButton:无法售出");
}
 
-(void)distribute{
    NSLog(@"SoldOutState-distribute:无法分发");
}
 
@end

售卖机(SaleMachine):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@interface SaleMachine : NSObject<NoMoneyDelegate,HasMoneyDelegate,SoldOutDelegate,SoldDelegate>
 
@property  (strong,nonatomic) NoMoneyState  *noMoneyState;
@property  (strong,nonatomic) HasMoneyState *hasMoneyState;
@property  (strong,nonatomic) SoldOutState  *soldOutState;
@property  (strong,nonatomic) SoldState     *soldState;
 
-(instancetype)initWithCount:(NSInteger)count;
 
-(void)setCurrentState:(State *)currentState;
 
-(void)putMoney;
-(void)ejectMoney;
-(void)pressButton;
 
@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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@interface SaleMachine()
 
@property (strong,nonatomic)  State *state;
@property (assign,nonatomic)  NSInteger  productCount;
 
@end
 
@implementation SaleMachine
 
-(instancetype)initWithCount:(NSInteger)count{
    self=[super init];
    if (self) {
        self.noMoneyState=[[NoMoneyState alloc]init];
        self.noMoneyState.delegate=self;
        self.hasMoneyState=[[HasMoneyState alloc]init];
        self.hasMoneyState.delegate=self;
        self.soldState=[[SoldState alloc]init];
        self.soldState.delegate=self;
        self.soldOutState=[[SoldOutState alloc]init];
        self.soldOutState.delegate=self;
        self.productCount=count;
        if (count) {
            self.state=self.noMoneyState;
        }
    }
    return self;
}
 
-(void)putMoney{
    [self.state putMoney];
}
 
-(void)ejectMoney{
    [self.state ejectMoney];
}
 
-(void)pressButton{
    [self.state pressButton];
    [self.state distribute];
}
 
-(void)setCurrentState:(State *)currentState{
    self.state=currentState;
}
#pragma mark - NoMoenyDelegate
-(State *)geHasMoneyState{
    return self.hasMoneyState;
}
 
#pragma mark - HasMoneyDelegate
-(State *)getNoMoneyState{
    return self.noMoneyState;
}
 
-(State *)getSoldState{
    return self.soldState;
}
#pragma mark - SoldDelegate
-(void)realseProduct{
    NSLog(@"SoldDelegate-realseProduct:商品售出");
    if (self.productCount) {
        self.productCount-=1;
    }
}
 
-(State *)getSoldOutState{
    return self.soldOutState;
}
 
-(NSInteger)getCurrentCount{
    return self.productCount;
}
 
@end

测试:

1
2
3
4
5
6
7
8
9
10
SaleMachine *machine=[[SaleMachine alloc]initWithCount:1];
[machine putMoney];
[machine ejectMoney];
 
[machine putMoney];
[machine pressButton];
 
SaleMachine *next=[[SaleMachine alloc]initWithCount:1];
[next putMoney];
[next ejectMoney];

测试效果:

  

状态模式的优缺点:

优点:状态模式允许一个对象基于内部状态有不同的行为,将行为委托给状态对象执行,状态转化可以由Context也可以由状态行为控制,比较灵活;

缺点: 状态模式的使用必然会增加系统类和对象的个数。状态模式的结构与实现都较为复杂,如果使用不当将导致程序结构和代码的混乱。

posted @   Fly_Elephant  阅读(1122)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示