状态模式 C++实现

#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;
/*
  state 模式: 三个角色: context class / state class / concreate state class  
  
*/
class work;       //前置声明 
// abstract state class   
class state
{
  public:
        virtual void writeprogram(work *w) = 0;    
};

// context class 

class work    //该类 实现对state class 的修改,选择具体的 state 
{
  private:
        state * current;   //将state  class 嵌入到  context 中以便对其进行修改 。 
        int hour;
  public:
        
        work(state *s):current(s),hour(0)
        {
           
        }
        
        void setstate(state *s)
        { delete current;
          current = s;    
        }
        int get_hour()
        {
          return hour;    
        }
        void set_hour(int value)
        {
          hour = value;    
        }
        void writeprogram()
        {
          current->writeprogram(this);    
        }
        
        
};
// concreate state class 
class rest: public state
{
  public:
        void writeprogram(work *w)
        {
          cout <<"now is"<<w->get_hour()<<"and is surposed to rest!"<< endl;    
        }   
};

class evening: public state
{
  public:
        void writeprogram(work *w)
        {
          if(w->get_hour() < 20)
          {
            cout <<"time is "<<w->get_hour()<<"evening now !"<< endl;      
          }    
          else
          {
            w->setstate(new rest()); 
             w->writeprogram();       
          }
        }    
};

class afternoon: public state
{
   public:
        void writeprogram(work *w)
        {
          if(w->get_hour() < 17)
          {
            cout <<"time is "<<w->get_hour()<<"afternoon go on!"<< endl;      
          }    
          else
          {
             w->setstate(new evening()); 
              w->writeprogram();       
          }
        }    
};

class midlenoon: public state
{
   public:
        void writeprogram(work *w) 
        {
          if(w->get_hour() < 14)
          {
            cout <<"time is "<<w->get_hour()<<" midlenoon rest time"<< endl;      
          }    
          else
          {
            w->setstate(new afternoon()); 
            w->writeprogram();       
          }
            
        }    
};

class forenoon: public state
{
  public:
        void writeprogram(work *w)
        {
          if(w->get_hour() < 12)
           {
              cout <<"now time is "<<w->get_hour()<<"forenoon "<<"state is very good!"<< endl;     
           }    
           else
           {
             w->setstate(new midlenoon());  // 修改状态 
             w->writeprogram();  // 调用下一个状态的writeprogram()函数,以下同理。 
            }
        }    
};

int main()
{
  work my_work(new forenoon());
  
  for(int i = 0; i < 30; i = i + 1)
  {
    my_work.set_hour(i);
    my_work.writeprogram();
  }  
  
  system("pause");
  return 0;    
}


 

 

总结:这个模式需要继续好好理解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值