-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinary_State.h
134 lines (118 loc) · 2.35 KB
/
Linary_State.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*****************************************************************/
/* 设计模式————状态模式
/* 作者:李凝瑞
/* 时间:2015.06.07
/*****************************************************************/
/** 状态模式:
*/
#pragma once
#include "stdafx.h"
namespace dp_state {
// 抽象状态类
class State {
public:
virtual void Handle() = 0;
};
// 具体状态类
class ConcreteState : public State {
public:
virtual void Hanlde() {
// 方法具体实现代码
}
};
// 环境类,是拥有多种状态的对象
class Context {
public:
// 状态设置
void SetState(State * state) {
this->state = state;
}
// 事务处理
void Request() {
// 调用状态对象的业务方法
state->Handle();
}
private:
State * state;
};
};
namespace linary_state {
// 抽象状态类
class State {
public:
State(int times) : times(times) {}
int GetTimes() const {
return times;
}
virtual void Display() = 0;
protected:
// 屏幕放大倍数
int times;
};
// 正常状态类
class NormalState : public State {
public:
NormalState() : State(1) {}
virtual void Display() {
std::cout << "正常大小" <<std::endl;
}
};
// 2倍状态类
class LargerState : public State {
public:
LargerState() : State(2) {}
virtual void Display() {
std::cout << "2 倍大小" <<std::endl;
}
};
// 4倍状态类
class LargestState : public State {
public:
LargestState() : State(4) {}
virtual void Display() {
std::cout << "4 倍大小" <<std::endl;
}
};
// 屏幕:环境类
class Screen {
public:
Screen() {
// 创建正常状态对象
state = new NormalState();
state->Display();
}
~Screen() {
delete state;
}
void SetState(State * state) {
this->state = state;
}
// 单击事件处理方法,封装了对状态类中业务方法的调用和状态的转换
void OnClick() {
State * temp = state;
if (state->GetTimes() == 1) {
SetState(new LargerState());
} else if (state->GetTimes() == 2) {
SetState(new LargestState());
} else {
SetState(new NormalState());
}
delete temp;
this->state->Display();
}
private:
// 持有的状态类
State * state;
};
// 测试代码
static void Test_State() {
std::cout << "--------------------------" << std::endl;
std::cout << "-- 状态模式测试 --" << std::endl;
std::cout << "--------------------------" << std::endl;
Screen screen;
screen.OnClick();
screen.OnClick();
screen.OnClick();
std::cout << std::endl;
}
};