首页 > 代码库 > 状态模式c++
状态模式c++
状态模式(State Pattern)是设计模式的一种,属于行为模式。
定义(源于Design Pattern):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判断逻辑简化。
意图:允许一个对象在其内部状态改变时改变它的行为
适用场景:
1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。
2.一个操作中含有庞大的多分支结构,并且这些分支决定于对象的状态。
headfirst状态模式实现(c++编写)
这个模式必须用.h文件(以前的不用,感觉有点麻烦)
下面是代码部分
gumballmachine.cpp
#include "gumballmachine.h"
#include"hasquarterstate.h"
#include"soldoutstate.h"
#include"noquarterstate.h"
#include"hasquarterstate.h"
#include"soldstate.h"
#include <iostream>
using namespace std;
int GumballMachine::count=0;
GumballMachine::GumballMachine(int numberGumballs)
{
hasQuarterState=new HasQuarterState();
soldOutState=new SoldOutState();
noQuarterState=new NoQuarterState();
soldState=new SoldState();
if(numberGumballs>0)
{
state=noQuarterState;
}
}
GumballMachine::~GumballMachine()
{
//dtor
}
void GumballMachine::releaseBall()
{
cout<<"A gumball comes rolling out the slot.."<<endl;
if(count!=0)
{
count=count-1;
}
}
void GumballMachine::ejectQuarter()
{
state->ejectQuarter();
}
void GumballMachine::insertQuarter()
{
state->insertQuarter();
}
void GumballMachine::turnCrank()
{
state->turnCrank();
state->dispense();
}
int GumballMachine::getCount()
{
return count;
}
void GumballMachine::setCount(int c)
{
count=c;
}
State *GumballMachine::getHasQuarterState()
{
return hasQuarterState;
}
State *GumballMachine::getNoQuaterState()
{
return noQuarterState;
}
State *GumballMachine::getSoldOutState()
{
return soldOutState;
}
void GumballMachine::setState(State *s)
{
this->state=s;
}
#include"hasquarterstate.h"
#include"soldoutstate.h"
#include"noquarterstate.h"
#include"hasquarterstate.h"
#include"soldstate.h"
#include <iostream>
using namespace std;
int GumballMachine::count=0;
GumballMachine::GumballMachine(int numberGumballs)
{
hasQuarterState=new HasQuarterState();
soldOutState=new SoldOutState();
noQuarterState=new NoQuarterState();
soldState=new SoldState();
if(numberGumballs>0)
{
state=noQuarterState;
}
}
GumballMachine::~GumballMachine()
{
//dtor
}
void GumballMachine::releaseBall()
{
cout<<"A gumball comes rolling out the slot.."<<endl;
if(count!=0)
{
count=count-1;
}
}
void GumballMachine::ejectQuarter()
{
state->ejectQuarter();
}
void GumballMachine::insertQuarter()
{
state->insertQuarter();
}
void GumballMachine::turnCrank()
{
state->turnCrank();
state->dispense();
}
int GumballMachine::getCount()
{
return count;
}
void GumballMachine::setCount(int c)
{
count=c;
}
State *GumballMachine::getHasQuarterState()
{
return hasQuarterState;
}
State *GumballMachine::getNoQuaterState()
{
return noQuarterState;
}
State *GumballMachine::getSoldOutState()
{
return soldOutState;
}
void GumballMachine::setState(State *s)
{
this->state=s;
}
gumballmachine.h
#ifndef GUMBALLMACHINE_H
#define GUMBALLMACHINE_H
#include"state.h"
class GumballMachine
{
public:
GumballMachine(int numberGumballs);
virtual ~GumballMachine();
void releaseBall();
void insertQuarter();
void ejectQuarter();
void turnCrank();
int getCount();
void setCount(int c);
State * getSoldOutState();
State * getNoQuaterState();
State* getHasQuarterState();
void setState(State *s);
protected:
private:
State *hasQuarterState;
State *noQuarterState;
State *soldOutState;
State *soldState;
State *state;
static int count;
};
#endif // GUMBALLMACHINE_H
#define GUMBALLMACHINE_H
#include"state.h"
class GumballMachine
{
public:
GumballMachine(int numberGumballs);
virtual ~GumballMachine();
void releaseBall();
void insertQuarter();
void ejectQuarter();
void turnCrank();
int getCount();
void setCount(int c);
State * getSoldOutState();
State * getNoQuaterState();
State* getHasQuarterState();
void setState(State *s);
protected:
private:
State *hasQuarterState;
State *noQuarterState;
State *soldOutState;
State *soldState;
State *state;
static int count;
};
#endif // GUMBALLMACHINE_H
hasquaterstate.cpp
#include "hasquarterstate.h"
#include <iostream>
using namespace std;
HasQuarterState::HasQuarterState(GumballMachine *g)
{
this->gumballmachine=g;
}
HasQuarterState::HasQuarterState()
{
}
HasQuarterState::~HasQuarterState()
{
//dtor
}
void HasQuarterState::dispense()
{
gumballmachine->releaseBall();
if(gumballmachine->getCount()>0)
{
gumballmachine->setState(gumballmachine->getNoQuaterState());
}
else
{
cout<<"Oops,out of gumballs!";
gumballmachine->setState(gumballmachine->getSoldOutState());
}
}
void HasQuarterState::ejectQuarter()
{
cout<<"Sorry,you already turned the crank"<<endl;
}
void HasQuarterState::insertQuarter()
{
cout<<"Please wait,we‘re already giving you a gumball"<<endl;
}
void HasQuarterState::turnCrank()
{
cout<<"Turning twice does‘t get you another gumball"<<endl;
}
#include <iostream>
using namespace std;
HasQuarterState::HasQuarterState(GumballMachine *g)
{
this->gumballmachine=g;
}
HasQuarterState::HasQuarterState()
{
}
HasQuarterState::~HasQuarterState()
{
//dtor
}
void HasQuarterState::dispense()
{
gumballmachine->releaseBall();
if(gumballmachine->getCount()>0)
{
gumballmachine->setState(gumballmachine->getNoQuaterState());
}
else
{
cout<<"Oops,out of gumballs!";
gumballmachine->setState(gumballmachine->getSoldOutState());
}
}
void HasQuarterState::ejectQuarter()
{
cout<<"Sorry,you already turned the crank"<<endl;
}
void HasQuarterState::insertQuarter()
{
cout<<"Please wait,we‘re already giving you a gumball"<<endl;
}
void HasQuarterState::turnCrank()
{
cout<<"Turning twice does‘t get you another gumball"<<endl;
}
noquarterstate.cpp
#include "noquarterstate.h"
#include <iostream>
using namespace std;
NoQuarterState::NoQuarterState(GumballMachine *g)
{
this->gumballmachine=g;
}
NoQuarterState::NoQuarterState()
{
}
NoQuarterState::~NoQuarterState()
{
//dtor
}
void NoQuarterState::dispense()
{
cout<<"You need to pay first"<<endl;
}
void NoQuarterState::ejectQuarter()
{
cout<<"Sorry,your haven‘t insert a quarter"<<endl;
}
void NoQuarterState::insertQuarter()
{
cout<<"You insert a quarter"<<endl;
gumballmachine->setState(gumballmachine->getHasQuarterState());
}
void NoQuarterState::turnCrank()
{
cout<<"you turned,but there‘s no quarter"<<endl;
}
#include <iostream>
using namespace std;
NoQuarterState::NoQuarterState(GumballMachine *g)
{
this->gumballmachine=g;
}
NoQuarterState::NoQuarterState()
{
}
NoQuarterState::~NoQuarterState()
{
//dtor
}
void NoQuarterState::dispense()
{
cout<<"You need to pay first"<<endl;
}
void NoQuarterState::ejectQuarter()
{
cout<<"Sorry,your haven‘t insert a quarter"<<endl;
}
void NoQuarterState::insertQuarter()
{
cout<<"You insert a quarter"<<endl;
gumballmachine->setState(gumballmachine->getHasQuarterState());
}
void NoQuarterState::turnCrank()
{
cout<<"you turned,but there‘s no quarter"<<endl;
}
noquarterstate.h
#ifndef NOQUARTERSTATE_H
#define NOQUARTERSTATE_H
#include "state.h"
#include"gumballmachine.h"
class NoQuarterState : public State
{
public:
NoQuarterState(GumballMachine *g);
NoQuarterState();
virtual ~NoQuarterState();
void insertQuarter();//投钱
void ejectQuarter();//退钱
void turnCrank();//转动曲柄...
void dispense();
protected:
private:
GumballMachine *gumballmachine;
};
#endif // NOQUARTERSTATE_H
#define NOQUARTERSTATE_H
#include "state.h"
#include"gumballmachine.h"
class NoQuarterState : public State
{
public:
NoQuarterState(GumballMachine *g);
NoQuarterState();
virtual ~NoQuarterState();
void insertQuarter();//投钱
void ejectQuarter();//退钱
void turnCrank();//转动曲柄...
void dispense();
protected:
private:
GumballMachine *gumballmachine;
};
#endif // NOQUARTERSTATE_H
soldoutstate.cpp
#include "soldoutstate.h"
#include <iostream>
using namespace std;
SoldOutState::SoldOutState(GumballMachine *g)
{
this->gumballmachine=g;
}
SoldOutState::SoldOutState()
{
}
SoldOutState::~SoldOutState()
{
//dtor
}
void SoldOutState::dispense()
{
cout<<"No gumball dispened"<<endl;
}
void SoldOutState::ejectQuarter()
{
cout<<"You can‘t eject,you have‘t inserter a quarter yet"<<endl;
}
void SoldOutState::insertQuarter()
{
cout<<"You can‘t insert a quarter,the machine is sold out"<<endl;
}
void SoldOutState::turnCrank()
{
cout<<"You turned,but there are no gumballs"<<endl;
}
soldoutstate.h
#include <iostream>
using namespace std;
SoldOutState::SoldOutState(GumballMachine *g)
{
this->gumballmachine=g;
}
SoldOutState::SoldOutState()
{
}
SoldOutState::~SoldOutState()
{
//dtor
}
void SoldOutState::dispense()
{
cout<<"No gumball dispened"<<endl;
}
void SoldOutState::ejectQuarter()
{
cout<<"You can‘t eject,you have‘t inserter a quarter yet"<<endl;
}
void SoldOutState::insertQuarter()
{
cout<<"You can‘t insert a quarter,the machine is sold out"<<endl;
}
void SoldOutState::turnCrank()
{
cout<<"You turned,but there are no gumballs"<<endl;
}
soldoutstate.h
#ifndef SOLDOUTSTATE_H
#define SOLDOUTSTATE_H
#include "state.h"
#include"gumballmachine.h"
class SoldOutState : public State
{
public:
SoldOutState(GumballMachine *g);
SoldOutState();
virtual ~SoldOutState();
void insertQuarter();//投钱
void ejectQuarter();//退钱
void turnCrank();//转动曲柄...
void dispense();
protected:
private:
GumballMachine *gumballmachine;
};
#endif // SOLDOUTSTATE_H
#define SOLDOUTSTATE_H
#include "state.h"
#include"gumballmachine.h"
class SoldOutState : public State
{
public:
SoldOutState(GumballMachine *g);
SoldOutState();
virtual ~SoldOutState();
void insertQuarter();//投钱
void ejectQuarter();//退钱
void turnCrank();//转动曲柄...
void dispense();
protected:
private:
GumballMachine *gumballmachine;
};
#endif // SOLDOUTSTATE_H
soldstate.cpp
#include "soldstate.h"
#include"state.h"
#include <iostream>
using namespace std;
SoldState::SoldState(GumballMachine *g)
{
this->gumballmachine=g;
}
SoldState::SoldState()
{
}
SoldState::~SoldState()
{
//dtor
}
void SoldState::dispense()
{
gumballmachine->releaseBall();
if(gumballmachine->getCount()>0)
{
gumballmachine->setState(gumballmachine->getSoldOutState());
}
}
void SoldState::ejectQuarter()
{
cout<<"Sorry,you already turned the crank"<<endl;
}
void SoldState::insertQuarter()
{
cout<<"Please wait,we‘re already giving you a gumball"<<endl;
}
void SoldState::turnCrank()
{
cout<<"Turning twice does‘t get you another gumball"<<endl;
}
#include"state.h"
#include <iostream>
using namespace std;
SoldState::SoldState(GumballMachine *g)
{
this->gumballmachine=g;
}
SoldState::SoldState()
{
}
SoldState::~SoldState()
{
//dtor
}
void SoldState::dispense()
{
gumballmachine->releaseBall();
if(gumballmachine->getCount()>0)
{
gumballmachine->setState(gumballmachine->getSoldOutState());
}
}
void SoldState::ejectQuarter()
{
cout<<"Sorry,you already turned the crank"<<endl;
}
void SoldState::insertQuarter()
{
cout<<"Please wait,we‘re already giving you a gumball"<<endl;
}
void SoldState::turnCrank()
{
cout<<"Turning twice does‘t get you another gumball"<<endl;
}
soldstate.h
#ifndef SOLDSTATE_H
#define SOLDSTATE_H
#include "state.h"
#include"gumballmachine.h"
class SoldState : public State
{
public:
SoldState(GumballMachine *g);
SoldState();
virtual ~SoldState();
void insertQuarter();//投钱
void ejectQuarter();//退钱
void turnCrank();//转动曲柄...
void dispense();
protected:
private:
GumballMachine *gumballmachine;
};
#endif // SOLDSTATE_H
#define SOLDSTATE_H
#include "state.h"
#include"gumballmachine.h"
class SoldState : public State
{
public:
SoldState(GumballMachine *g);
SoldState();
virtual ~SoldState();
void insertQuarter();//投钱
void ejectQuarter();//退钱
void turnCrank();//转动曲柄...
void dispense();
protected:
private:
GumballMachine *gumballmachine;
};
#endif // SOLDSTATE_H
state.cpp
#include "state.h"
State::State()
{
//ctor
}
State::~State()
{
//dtor
}
State::State()
{
//ctor
}
State::~State()
{
//dtor
}
state.h
#ifndef STATE_H
#define STATE_H
class State
{
public:
State();
virtual ~State();
virtual void insertQuarter()=0;//投钱
virtual void ejectQuarter()=0;//退钱
virtual void turnCrank()=0;//转动曲柄...
virtual void dispense()=0;//发放糖果...
};
#endif // STATE_H
#define STATE_H
class State
{
public:
State();
virtual ~State();
virtual void insertQuarter()=0;//投钱
virtual void ejectQuarter()=0;//退钱
virtual void turnCrank()=0;//转动曲柄...
virtual void dispense()=0;//发放糖果...
};
#endif // STATE_H
main.cpp
#include <iostream>
#include"state.h"
#include"gumballmachine.h"
#include"hasquarterstate.h"
#include"noquarterstate.h"
#include"soldoutstate.h"
#include"soldstate.h"
using namespace std;
int main()
{
GumballMachine *gumballMachine=new GumballMachine(5);
gumballMachine->setCount(10);
cout<<gumballMachine->getCount()<<endl;
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
return 0;
}
#include"state.h"
#include"gumballmachine.h"
#include"hasquarterstate.h"
#include"noquarterstate.h"
#include"soldoutstate.h"
#include"soldstate.h"
using namespace std;
int main()
{
GumballMachine *gumballMachine=new GumballMachine(5);
gumballMachine->setCount(10);
cout<<gumballMachine->getCount()<<endl;
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
return 0;
}
运行结果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。