状态模式

#include <iostream>
using namespace std;
class PhoneState;
//定义电话类 
class Phone
{
      private:
              PhoneState *state;//记录状态 
      public:
             Phone(PhoneState *_state){this->state=_state;};
             void setState(PhoneState *_state);//设置状态 
             void openPhone();//开机 
             void closePhone();//关机 
             void callPhone();//打电话 
             void hangPhone(); //挂电话      
};
//定义状态接口 
class PhoneState
{
      public:
             virtual void open(Phone *_phone){cout<<"It's not available for open!"<<endl;};
             virtual void close(Phone *_phone){cout<<"It's not available for close!"<<endl;};
             virtual void call(Phone *_phone){cout<<"It's not available for call!"<<endl;};
             virtual void hang(Phone *_phone){cout<<"It's not available for hang!"<<endl;};
};
//定义忙状态,正在通话中 
class BusyState:public PhoneState
{
      public:
             BusyState(){cout<<"The Phone Is Busy Now!"<<endl;};
             void hang(Phone *_phone);           
};
//定义闲状态,待机 
class FreeState:public PhoneState
{
       public:
             FreeState(){cout<<"The Phone Is Free Now!"<<endl;}; 
             void call(Phone *_phone);
             void close(Phone *_phone); 
};
//定义关机状态 
class CloseState:public PhoneState
{
       public:
              CloseState(){cout<<"The Phone Is Closed Now!"<<endl;};
              void open(Phone *_phone);  
};
//挂电话行为 
void BusyState::hang(Phone *_phone)
{
     cout<<"The Phone Is Hanged!"<<endl;
     _phone->setState(new FreeState());
     delete this;
}

//打电话行为 
void FreeState::call(Phone *_phone)
{
      cout<<"The Phone Is Called!"<<endl;
     _phone->setState(new BusyState());
     delete this;
}
//关机行为 
void FreeState::close(Phone *_phone)
{
      cout<<"The Phone Is Closed!"<<endl;
     _phone->setState(new CloseState());
     delete this;
}
//开机行为      
void CloseState::open(Phone *_phone)
{
     cout<<"The Phone Is Opened!"<<endl;
     _phone->setState(new FreeState());
     delete this; 
}
//改变电话状态 
void Phone::setState(PhoneState *_state)
{
     this->state=_state;
}
//打电话 
void Phone::callPhone()
{
     this->state->call(this);
}
//关电话 
void Phone::closePhone()
{
     this->state->close(this);
}
//挂机 
void Phone::hangPhone()
{
     this->state->hang(this);
}
//开机 
void Phone::openPhone()
{
     this->state->open(this);
}

int main()
{
    Phone *phone=new Phone(new FreeState());
    int i;
    while (1){
    cin>>i;
       if(i==1)    phone->openPhone();
       if(i==2)    phone->closePhone();
       if(i==3)    phone->callPhone();
       if(i==4)    phone->hangPhone();
}
}

状态模式另外一种很好的代码 。

1. 在内部状态改变的时候,改变它的行为。

2. Context将与状态相关的操作委托给当前的Concrete State对象处理。

3. Context可将自身作为一个参数传递给处理该请求的状态对象。这使得状态对象在必要时可访问Context。

4. Context或Concrete State类都可决定哪个状态是另外哪一个的后继者,以及是在何种条件下进行状态转换。也就是说可以在State中保存对Concrete State的引用,在必要时设置具体的状态,做到状态的转换。

5. 一般来讲,当状态转换是固定的时候,状态转换就适合放在Context中。然而,当转换是更动态的时候,通常会放到具体的状态类中进行。(具体状态类持有Context的引用,实现状态的转换)。

posted @ 2012-11-21 11:26  南屏晚钟  阅读(167)  评论(0编辑  收藏  举报