forked from yogykwan/design-patterns-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.cc
More file actions
52 lines (43 loc) · 1.02 KB
/
state.cc
File metadata and controls
52 lines (43 loc) · 1.02 KB
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
//
// Created by Jennica on 2017/1/2.
//
#include "state.h"
#include <iostream>
Work::Work() {
state_ = new WorkingState();
}
Work::~Work() {
delete state_;
}
void Work::SetState(State *state) {
delete state_;
state_ = state;
}
void Work::WriteProgram() {
state_->WriteProgram(this);
}
void WorkingState::WriteProgram(Work *work) {
if(work->hour_ < 17) {
std::cout << work->hour_ << " : working" << std::endl;
} else {
work->SetState(new OvertimeState());
work->WriteProgram();
}
}
void OvertimeState::WriteProgram(Work *work) {
if(work->finished_) {
work->SetState(new RestState());
work->WriteProgram();
} else if (work->hour_ < 21) {
std::cout << work->hour_ << " : overtime" << std::endl;
} else {
work->SetState(new SleepingState());
work->WriteProgram();
}
}
void RestState::WriteProgram(Work *work) {
std::cout << work->hour_ << " : return to rest" << std::endl;
}
void SleepingState::WriteProgram(Work *work) {
std::cout << work->hour_ << " : sleeping" << std::endl;
}