国家模式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谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
随机推荐
- IIS7配置PHP图解
IIS6整合PHP详解:http://zhidao.zgsj.com/article/8/2011118142648.shtml PHP5.2.17 官方下载: http://windows.php. ...
- ZOJ 1136 Longest Ordered Subsequence DP
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1136 题目大意:给定一串序列,求最长的升序列长度,如1, 7, 3, ...
- Android自定义组件系列【11】——实现3D立体旋转效果
今天在网上看到一篇文章写关于Android实现3D旋转(http://www.ibm.com/developerworks/cn/opensource/os-cn-android-anmt2/inde ...
- 2020发布 .NET 5 下一代全平台 .Net 框架
[翻译] 正式宣布 .NET 5 2019-05-07 01:18 by Rwing, 16515 阅读, 79 评论, 收藏, 编辑 原文: Introducing .NET 5 今天,我们宣布 . ...
- win-server下定时备份oracle数据库实现方法
1.在e盘建立一个“oracle_backup”的文件夹,文件夹下再建两个文件夹分别为:“new”,“old”,然后在oracle_backup文件夹中新建一个bat文件,取名“expdb.bat”, ...
- wPaint在线绘图插件
wPaint在线绘图插件 一.总结 一句话总结: 1.搜画图插件的时候关键词应该搜什么? jquery画图插件 js画图插件 jquery绘图插件 这些 二.在线绘图插件--wPaint 的实际应用 ...
- 学习Numpy基础操作
# coding:utf-8 import numpy as np from numpy.linalg import * def day1(): ''' ndarray :return: ''' ls ...
- 用Java对CSV文件进行读写操作
需要jar包:javacsv-2.0.jar 读操作 // 读取csv文件的内容 public static ArrayList<String> readCsv(String filepa ...
- [Compose] 8. A curated collection of Monoids and their uses
const { List } = require('immutable-ext'); const Right = x => ({ chain : f => f(x), ap : other ...
- [Angular Directive] Write a Structural Directive in Angular 2
Structural directives enable you to use an element as a template for creating additional elements. C ...