/* 小鱼号的代码日志
* 设计模式
* 状态模式
* 解决对象在多种状态转换时,需要对外输出不同的行为的问题。
* 状态和行为时一一对应的
* 当一个对象内在状态改变时,允许改变其行为.
* 这个对象看起来像是改变了其类
* 现实实例:
* 抽奖的几种状态
* 不能抽奖状态(需要先扣积分)
* 可以抽奖(扣除了积分)
* 领取奖品
* 奖品领完了(活动结束)
*/
#include<iostream>
#include<list>
#include<map>
#include<stdlib.h>
#include "stateactivity.h"
using namespace std;
class Activity;
///抽奖状态抽象类
class State
{
public:
State(Activity* act)
{
m_act = act;
}
public:
//扣除积分
virtual void dedcutMoney() = 0;
//是否抽中奖品
virtual bool raffle() = 0;
//发放奖品
virtual void dispensePrize() = 0;
protected:
Activity* m_act;
};
///不能抽奖的状态
class NoRaffleState : public State
{
public:
NoRaffleState(Activity* act):State(act)
{
}
public:
//扣除积分 将状态设置成可抽奖状态
void dedcutMoney()
{
cout << "dedcut 50 and can raffle" << endl;
m_act->setState(m_act->getCanRaffleState());
}
//是否抽中奖品 当前状态不能抽奖
bool raffle()
{
cout << "can not raffle please dedcut first" << endl;
}
//发放奖品 前状态发放奖品
void dispensePrize()
{
cout << "can not dispecse prize" << endl;
}
};
///可以抽奖的状态
class CanRaffleState : public State
{
public:
CanRaffleState(Activity* act):State(act)
{
}
public:
//扣除积分
void dedcutMoney()
{
cout << "dedcut 50 already" << endl;
}
//是否抽中奖品
bool raffle()
{
cout << "hold on raffle" << endl;
int a = rand() % 10;
if(a %2 == 0)
{
m_act->setState(m_act->getDispenseRaffleState());
cout << "congratulate" << endl;
return true;
}
else
{
m_act->setState(m_act->getNoRaffleState());
cout << "sorry" << endl;
return false;
}
}
//发放奖品
void dispensePrize()
{
cout << "can not dispecse prize" << endl;
}
};
///发放奖品的状态
class DispenseRaffleState : public State
{
public:
DispenseRaffleState(Activity* act):State(act)
{
}
public:
//扣除积分
void dedcutMoney()
{
cout << "dedcut 50 already" << endl;
}
//是否抽中奖品
bool raffle()
{
cout << "raffle already" << endl;
return false;
}
//发放奖品
void dispensePrize()
{
if(m_act->getPrizeCount() > 0)
{
m_act->setState(m_act->getNoRaffleState());
cout << "take your prize" << endl;
}
else
{
m_act->setState(m_act->getRaffleOutState());
cout << "sorry the prize is out" << endl;
}
}
};
///奖品发完了的状态 活动已经结束
class RaffleOutState : public State
{
public:
RaffleOutState(Activity* act):State(act)
{
}
public:
//扣除积分
void dedcutMoney()
{
cout << "sorry prize already out activirt is over" << endl;
}
//是否抽中奖品
bool raffle()
{
cout << "sorry prize already out activirt is over" << endl;
return false;
}
//发放奖品
void dispensePrize()
{
cout << "sorry prize already out activirt is over" << endl;
}
};
#ifndef STATEACTIVITY_H
#define STATEACTIVITY_H
class State;
class Activity
{
public:
Activity(int count);
public:
//扣除积分
void dedcutMoney();
//是否抽中奖品
bool raffle();
//发放奖品
void dispensePrize();
State* getNoRaffleState();
State* getCanRaffleState();
State* getDispenseRaffleState();
State* getRaffleOutState();
State* setState(State* state);
int getPrizeCount();
private:
int m_prizeCount;
State* m_currentState;
State* m_noRaffleState;
State* m_canRaffleState;
State* m_dispenseRaffleState;
State* m_raffleOutState;
};
#endif // STATEACTIVITY_H
#include "state.cpp"
///含所有的状态对象
///各个状态子类也含有Activity对象
Activity::Activity(int count)
{
m_prizeCount = count;
m_noRaffleState = new NoRaffleState(this);
m_canRaffleState = new CanRaffleState(this);
m_dispenseRaffleState = new DispenseRaffleState(this);
m_raffleOutState = new RaffleOutState(this);
m_currentState = getNoRaffleState();
}
void Activity::dedcutMoney()
{
m_currentState->dedcutMoney();
}
//是否抽中奖品
bool Activity::raffle()
{
if(m_currentState->raffle())
{
m_currentState->dispensePrize();
}
}
//发放奖品
void Activity::dispensePrize()
{
m_currentState->dispensePrize();
}
State* Activity::getNoRaffleState()
{
return m_noRaffleState;
}
State* Activity::getCanRaffleState()
{
return m_canRaffleState;
}
State* Activity::getDispenseRaffleState()
{
return m_dispenseRaffleState;
}
State* Activity::getRaffleOutState()
{
return m_raffleOutState;
}
State* Activity::setState(State* state)
{
m_currentState = state;
}
int Activity::getPrizeCount()
{
cout << "current prize count:" << m_prizeCount << endl;
return m_prizeCount--;
}
///状态模式
void testState()
{
cout << "state patterns" << endl;
Activity act(5);
for(int i = 0; i < 20; i++)
{
act.dedcutMoney();
act.raffle();
}
}
【C++设计模式】 状态模式
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 状态模式 一、描述 概念:允许一个对象在其内部状态改变时改变它的行为。 对象看起来似乎修改了它的类。 问题: 每个...
- 23种设计模式 “对象性能”模式 面向对象很好的解决了“抽象”的问题,但是必不可免地要付出一定的代价。对于通常情况...
- 23种设计模式 “对象性能”模式 面向对象很好的解决了“抽象”的问题,但是必不可免地要付出一定的代价。对于通常情况...