//state.h
#ifndef __STATE_H__
#define __STATE_H__
#include "context.h"
#include <iostream>
using namespace std;
typedef enum
{
CHILDREN = 1,
PRIMARY_SCHOOL_STUDENT,
MIDDLE_SCHOOL_STUDENT,
UNIVERSITY_SCHOOL_STUDENT,
GRADUATE_STUDENT,
PROGRAMER,
} EDUCATION_TYPE_EN;
class Life;
class State
{
public:
State() {}
virtual void Operation(Life *life) {};
};
class Children : public State
{
public:
Children(){}
void Operation(Life *life);
};
class PrimarySchoolStudent : public State
{
public:
void Operation(Life *life);
};
class MiddleSchoolStudent : public State
{
public:
void Operation(Life *life);
};
class UniversitySchoolStudent : public State
{
public:
void Operation(Life *life);
};
class GraduateStudent : public State
{
public:
void Operation(Life *life);
};
class Programmer : public State
{
public:
void Operation(Life *life);
};
#endif /* __STATE_H__ */
//state.cpp
#include "stdafx.h"
#include "state.h"
void Children::Operation(Life *life)
{
if (life->GetAge() < 0)
{
cout << life->GetAge() << "岁时" << "未出生" << endl;
}
else if (life->GetAge() >= 0 && life->GetAge() < 6)
{
cout << life->GetAge() << "岁时" << "未上学" << endl;
}
else
{
life->SetState(new PrimarySchoolStudent);
life->ShowState();
}
}
void PrimarySchoolStudent::Operation(Life *life)
{
if (life->GetAge() >= 6 && life->GetAge() < 12)
{
cout << life->GetAge() << "岁时" << "读小学" << endl;
}
else
{
life->SetState(new MiddleSchoolStudent);
life->ShowState();
}
}
void MiddleSchoolStudent::Operation(Life *life)
{
if (life->GetAge() >= 12 && life->GetAge() < 18)
{
cout << life->GetAge() << "岁时" << "读中学" << endl;
}
else
{
life->SetState(new UniversitySchoolStudent);
life->ShowState();
}
}
void UniversitySchoolStudent::Operation(Life *life)
{
if (life->GetAge() >= 18 && life->GetAge() < 22)
{
cout << life->GetAge() << "岁时" << "读大学" << endl;
}
else
{
life->SetState(new GraduateStudent);
life->ShowState();
}
}
void GraduateStudent::Operation(Life *life)
{
if (life->GetAge() >= 22 && life->GetAge() < 25)
{
cout << life->GetAge() << "岁时" << "读研究生" << endl;
}
else
{
life->SetState(new Programmer);
life->ShowState();
}
}
void Programmer::Operation(Life *life)
{
if (life->GetAge() >= 25)
{
cout << life->GetAge() << "岁时" << "成为码农,开始工作" << endl;
}
else
{
cout << "状态异常" << endl;
}
}
//context.h
#ifndef __CONTEXT_H__
#define __CONTEXT_H__
#include "state.h"
#include <iostream>
using namespace std;
class State;
class Life
{
private:
int age;
State *current;
public:
Life();
void SetState(State *state);
void SetAge(int age);
int GetAge() const;
void ShowState();
};
#endif /* __CONTEXT_H__ */
//context.cpp
#include "stdafx.h"
#include "context.h"
Life::Life()
{
current = new Children; //初始化最初状态
}
void Life::SetState(State *state)
{
current = state;
}
void Life::SetAge(int age)
{
this->age = age;
}
int Life::GetAge() const
{
return this->age;
}
void Life::ShowState()
{
current->Operation(this);
}
//test.cpp
#include "stdafx.h"
#include <iostream>
#include "state.h"
#include "context.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Life *pLife = new Life;
pLife->SetAge(5);
pLife->ShowState();
pLife->SetAge(10);
pLife->ShowState();
pLife->SetAge(16);
pLife->ShowState();
pLife->SetAge(18);
pLife->ShowState();
pLife->SetAge(21);
pLife->ShowState();
pLife->SetAge(23);
pLife->ShowState();
pLife->SetAge(28);
pLife->ShowState();
return 0;
}
状态模式之C++实现