国家模式c++
把状态的推断逻辑转移到表示不同状态的一系列类中,能够把复杂的推断逻辑简化。
#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;
}
#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
#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;
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;
}
#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
#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
#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
#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;
}
#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::State()
{
//ctor
}
State::~State()
{
//dtor
}
#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
#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;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
国家模式c++的更多相关文章
- 大话设计模式C++达到-文章16章-国家模式
一.UML画画 二.概念 状态模式(State):当一个对象的内在状态改变时同意改变其行为.这个对象看起来像是改变了其类. 三.说明 以下是来自书本和网络的对状态模式的定义和分析: (1)状态模式同意 ...
- SAP增强 和VA01相关增强点介绍
-转 sap寻找用户出口方法 sap的用户出口总共有三代: 一.User EXIT 第一代的用户出口,它们include在SAP标准程序的源代码里,可以说他们是源代码的一部分,你改了这种出口就相当于改 ...
- 一点公益商城开发系统模式Ring Buffer+
一个队列如果只生产不消费肯定不行的,那么如何及时消费Ring Buffer的数据呢?简单的方案就是当Ring Buffer"写满"的时候一次性将数据"消费"掉. ...
- 从接口、抽象类到工厂模式再到JVM来总结一些问题
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 涉及到的知识点总结如下: 为什么使用接口? 接口和抽象类的区别 简单工厂模式总结 Java中new和newInstance的区别 J ...
- 高访问量WEB开发中的架构模式,学习从点滴开始
当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构 ...
- Java设计模式之工厂模式(Factory)
前言: 前面学习了建造者设计模式,接下来学习一下Retrofit中使用的另外一个设计模式,工厂设计模式!!!里面采用工厂模式使得数据转换得到完全解耦,工厂模式的好处用到了极致,如此好的设计模式我们怎能 ...
- 两种交换机配置模式,以配置基于端口划分的VLAN为例
关于交换机的配置模式,大体上可以分为两类:其一以CISCO交换机为代表的配置模式,其二以Huawei.H3C交换机为代表的配置模式.其实这两种配置模式并没有本质的不同,只是配置的命令名称和配置方式存在 ...
- 在WildFly中运行多个standalone模式的实例
WildFly作为一款优秀的EJB容器,其前身为JBoss AS.JBoss作为一款开源的应用服务器,被广泛的应用在各种项目当中.假设我们现在有这样一个项目,他是以standalone的模式运行在 ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
随机推荐
- [array] leetCode-18. 4Sum -Medium
18. 4Sum -Medium descrition Given an array S of n integers, are there elements a, b, c, and d in S s ...
- 判断客户端是iPad、安卓还是ios
武穆逸仙 有人心疼时,眼泪才是眼泪,否则只是带着咸味的液体:被人呵护着,撒娇才是撒娇,要不然就是作死. 努力做一个可爱的人,不埋怨谁,不嘲笑谁,也不羡慕谁,阳光下灿烂,风雨中奔跑,做自己的梦,走自己的 ...
- 【Debug】— C++ 表达式必须包含类类型
错误一般发生在使用.进行访问时,原因可能在于: 你以为你定义了一个类对象,其实你是声明了一个函数,在编译器看来: 对类对象指针采用.的方式访问其成员变量: 也包括基本类型变量,错误地使用. int a ...
- windows2003 IIS6下安装ISAPI_Rewrite3破解版
摘抄的https://jingyan.baidu.com/article/ff42efa931a2c0c19e220298.html 非常感谢,我是怕百度经验有一天消失了,以防万一 iis6 ISAP ...
- Java内部抛出异常外部不能catch问题分析
今天在论坛看到一篇关于异常处理的文章,异常处理机制详解开头就搬出了这样一个例子: public class TestException { public TestException() { } boo ...
- Visual Studio 项目目录下的bin目录和 obj目录
一.Bin目录 Visual Studio 编译时,在bin 目录下有debug 和 release 目录. 1.Debug: 通常称为调试版本,它包含调试信息,所以要比Release 版本大很多(可 ...
- URL validation failed. The error could have been caused through the use of the browser's navigation
URL validation failed. The error could have been caused through the use of the browser's navigation ...
- oracle员工表和部门表基本操作
emp 员工表(empno 员工号/ename 员工姓名/job 工作/mgr 上级编号/hiredate 受雇日期/sal 薪金/comm 佣金/deptno 部门编号) dept 部门表(dept ...
- php实现 密码验证合格程序(复杂问题分类,超简单的)(分类+规范编码)
php实现 密码验证合格程序(复杂问题分类,超简单的)(分类+规范编码) 一.总结 一句话总结:复杂问题分类,超简单的.分类+规范编码. 1.写的时候判断 不能有相同长度超2的子串重复 的时候,子 ...
- [React] Test friendly approach
Add functional function such as change state, this should have tests covered. For example, in a comp ...