C/C++培训
达内IT学院
400-996-5531
[cpp] view plaincopy
#include
#include
class CContext;
class IState
{
public:
virtual void CurrentState(CContext* Change)=0;
};
class CContext
{
public:
CContext(IState* pState)
:m_state(pState),m_Change(0){}
~CContext(){delete m_state; m_state=NULL;}
void SetChange(int change){m_Change = change;}
int GetChange(){return m_Change;}
void SetState(IState* pState){delete m_state; m_state=pState;}
void ChangeState(){m_state->CurrentState(this);}
private:
IState* m_state;
int m_Change;
};
class CThird : public IState
{
void Third(CContext* Change)
{
printf("third end!\n");
}
virtual void CurrentState(CContext* Change)
{
Third(Change);
}
};
class CSecond : public IState
{
void Second(CContext* Change)
{
if (Change->GetChange()<5)
{
static int count = 1;
printf("second [%d]!\n", count++);
}
else
{
Change->SetState(new CThird());
Change->ChangeState();
}
}
virtual void CurrentState(CContext* Change)
{
Second(Change);
}
};
class CFirst : public IState
{
public:
void First(CContext* Change)
{
if ((Change->GetChange())<3)
{
static int count = 1;
printf("first [%d]!\n", count++);
}
else
{
Change->SetState(new CSecond());
Change->ChangeState();
}
}
virtual void CurrentState(CContext* Change)
{
First(Change);
}
};
int main(int argc, char* argv)
{
CContext* Change = new CContext(new CFirst());
for (int i=0; i<6; ++i)
{
Change->SetChange(i);
Change->ChangeState();
}
delete Change;
Change = NULL;
system("pause");
}
填写下面表单即可预约申请免费试听!怕钱不够?可就业挣钱后再付学费! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有
Tedu.cn All Rights Reserved