#ifndef __STATE_H__
#define __STATE_H__ #include <iostream>
#include<stdlib.h>
using namespace std;
class GumballMachine; class State
{
public:
State();
virtual ~State();
virtual void insertQuarter();
virtual void ejectQuarter();
virtual void turnCrank();
virtual void dispense();
}; class NoQuarterState : public State
{
private:
GumballMachine *gumballmachine;
public:
NoQuarterState(GumballMachine *g);
virtual ~NoQuarterState();
virtual void insertQuarter();
virtual void ejectQuarter();
virtual void turnCrank();
virtual void dispense(); }; class SoldOutState :public State
{
private:
GumballMachine *gumballmachine;
public:
SoldOutState(GumballMachine *g);
virtual ~SoldOutState();
virtual void insertQuarter();
virtual void ejectQuarter();
virtual void turnCrank();
virtual void dispense();
}; class HasQuarterState :public State
{
private:
GumballMachine *gumballmachine;
public:
HasQuarterState(GumballMachine *g);
virtual ~HasQuarterState();
virtual void insertQuarter();
virtual void ejectQuarter();
virtual void turnCrank();
virtual void dispense();
}; class SoldState :public State
{
private:
GumballMachine *gumballmachine;
public:
SoldState(GumballMachine *g);
virtual ~SoldState();
virtual void insertQuarter();
virtual void ejectQuarter();
virtual void turnCrank();
virtual void dispense();
}; class WinnerState :public State
{
private:
GumballMachine *gumballmachine;
public:
WinnerState(GumballMachine *g);
virtual ~WinnerState();
virtual void insertQuarter();
virtual void ejectQuarter();
virtual void turnCrank();
virtual void dispense();
}; #endif
#include "State.h"
#include "GumballMachine.h"
void State::dispense()
{ } void State::turnCrank()
{ } void State::ejectQuarter()
{ } void State::insertQuarter()
{ } State::~State()
{ } State::State()
{ }
NoQuarterState::~NoQuarterState()
{ } NoQuarterState::NoQuarterState(GumballMachine *g)
{
gumballmachine = g;
} void NoQuarterState::dispense()
{
cout << "You need to pay first." << endl;
} void NoQuarterState::turnCrank()
{
cout << "You turned, but there is no quarter." << endl;
} void NoQuarterState::ejectQuarter()
{
cout << "You has not insert quarter." << endl;
} void NoQuarterState::insertQuarter()
{
cout << "You insert a quarter." << endl;
gumballmachine->SetState(gumballmachine->GetHasQuarterState());
} void SoldOutState::dispense()
{
cout << "Sold out." << endl;
} void SoldOutState::turnCrank()
{
cout << "Sold out, there is no quarter." << endl;
} void SoldOutState::ejectQuarter()
{
cout << "Sold out, can't eject quarter." << endl;
} void SoldOutState::insertQuarter()
{
cout << "Sold out, You can't insert a quarter." << endl;
} SoldOutState::~SoldOutState()
{ } SoldOutState::SoldOutState(GumballMachine *g)
{
gumballmachine = g;
} void HasQuarterState::dispense()
{
cout << "No gum ball dispensed." << endl;
} void HasQuarterState::turnCrank()
{
cout << "You turned." << endl;
int winner = (rand() % 10);
if (winner == 0 && gumballmachine->getCount() > 1)
{
gumballmachine->SetState(gumballmachine->GetWinnerState());
}
else
{
gumballmachine->SetState(gumballmachine->GetSoldState());
}
} void HasQuarterState::ejectQuarter()
{
cout << "Quarter returned." << endl;
gumballmachine->SetState(gumballmachine->GetNoQuarterState());
} void HasQuarterState::insertQuarter()
{
cout << "You can't insert another quarter." << endl;
} HasQuarterState::~HasQuarterState()
{ } HasQuarterState::HasQuarterState(GumballMachine *g)
{
gumballmachine = g;
} void SoldState::dispense()
{
gumballmachine->releaseBall();
if (gumballmachine->getCount() > 0)
{
gumballmachine->SetState(gumballmachine->GetNoQuarterState());
}
else
{
cout << "Oops, out of gum balls!" << endl;
gumballmachine->SetState(gumballmachine->GetSoldOutState());
}
} void SoldState::turnCrank()
{
cout << "Turning twice does not get you another gum ball." << endl;
} void SoldState::ejectQuarter()
{
cout << "Sorry, you already turned the crank." << endl;
} void SoldState::insertQuarter()
{
cout << "Please wait, we are already giving you a gum ball." << endl;
} SoldState::~SoldState()
{ } SoldState::SoldState(GumballMachine *g)
{
gumballmachine = g;
} void WinnerState::dispense()
{
cout << "YOU ARE A WINNER, You get two gum balls." << endl;
gumballmachine->releaseBall();
if (gumballmachine->getCount() == 0)
{
gumballmachine->SetState(gumballmachine->GetSoldOutState());
}
else
{
gumballmachine->releaseBall();
if (gumballmachine->getCount() == 0)
{
gumballmachine->SetState(gumballmachine->GetSoldOutState());
}
else
{
gumballmachine->SetState(gumballmachine->GetNoQuarterState());
}
}
} void WinnerState::turnCrank()
{
cout << "Turning twice does not get you another gum ball." << endl;
} void WinnerState::ejectQuarter()
{
cout << "Sorry, you already turned the crank." << endl;
} void WinnerState::insertQuarter()
{
cout << "Please wait, we are already giving you a gum ball." << endl;
} WinnerState::~WinnerState()
{ } WinnerState::WinnerState(GumballMachine *g)
{
gumballmachine = g;
}
#ifndef __GUMBALLMACHINE_H__
#define __GUMBALLMACHINE_H__
#include "State.h"
#include <iostream>
#include<stdlib.h>
using namespace std;
//class State;
class GumballMachine
{
private:
State *soldOutState;
State *noQuarterState;
State *hasQuarterState;
State *soldState;
State *winnerState;
State *CurrentState;
int Count;
public:
GumballMachine(int num);
~GumballMachine(); void insertQuarter(); void ejectQuarter(); void turnCrank(); void SetState(State *st); State * GetNoQuarterState(); State * GetWinnerState(); State * GetHasQuarterState(); State * GetSoldState(); State * GetSoldOutState();
void releaseBall();
int getCount();
};
#endif
#include "GumballMachine.h"

int GumballMachine::getCount()
{
return Count;
} void GumballMachine::releaseBall()
{
cout << "A ball comes rolling out the slot." << endl;
if (Count != 0)
{
Count--;
}
} State * GumballMachine::GetSoldOutState()
{
return soldOutState;
} State * GumballMachine::GetSoldState()
{
return soldState;
} State * GumballMachine::GetHasQuarterState()
{
return hasQuarterState;
} State * GumballMachine::GetWinnerState()
{
return winnerState;
} State * GumballMachine::GetNoQuarterState()
{
return noQuarterState;
} void GumballMachine::SetState(State *st)
{
CurrentState = st;
} void GumballMachine::turnCrank()
{
CurrentState->turnCrank();
CurrentState->dispense();
} void GumballMachine::ejectQuarter()
{
CurrentState->ejectQuarter();
} void GumballMachine::insertQuarter()
{
CurrentState->insertQuarter();
} GumballMachine::~GumballMachine()
{ } GumballMachine::GumballMachine(int num)
{
Count = num;
soldOutState = new SoldOutState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
winnerState = new WinnerState(this);
soldState = new SoldState(this);
if (Count > 0)
{
CurrentState = noQuarterState;
}
}
#include <iostream>
#include "GumballMachine.h"
using namespace std;
int main()
{
GumballMachine *g = new GumballMachine(10);
for (int i = 0; i < 11; i ++)
{
cout << "================================" << endl;
g->insertQuarter();
g->turnCrank();
}
return 0;
}

:状态模式:GumballMachine的更多相关文章

  1. Head First 设计模式 --10 状态模式

    状态模式:允许对象在内部状态改变时改变他的行为,对象看起来好像修改了他的类. 用到的设计原则1.封装变化2.多用组合,少用继承3.针对接口编程,不针对实现编程4.松耦合5.对扩展开放,对修改关闭6.依 ...

  2. 《Head First 设计模式》学习笔记——状态模式

    在软件开发过程中.应用程序可能会依据不同的情况作出不同的处理. 最直接的解决方式是将这些全部可能发生的情况全都考虑到.然后使用if... ellse语句来做状态推断来进行不同情况的处理. 可是对复杂状 ...

  3. C#设计模式之10:状态模式

    状态模式 状态模式将合适的Context(上下文)模拟成一个状态机,在这个状态机的内部,由Context来决定整个状态机的状态,再根据不同的状态执行不同的方法.在C#编译器的帮助下,很多语法糖的背后都 ...

  4. Head First设计模式——状态模式

    糖果机 如下糖果机工作状态图,我们对这个状态图进行编码实现糖果机的工作过程 这个状态图的每个圆圈代表一个状态,可以看到有4个状态同时又4个动作,分别是:“投入1元钱”.“退回1元钱”.“转动曲柄”.“ ...

  5. 《Head First 设计模式》:状态模式

    正文 一.定义 状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类. 要点: 状态模式允许一个对象基于内部状态而拥有不同的行为. 状态模式将状态封装成为独立的类,并将动作委托到代 ...

  6. StatePattern(状态模式)

    /** * 状态模式 * @author TMAC-J * 状态模式和策略模式很像,其实仔细研究发现完全不一样 * 策略模式各策略之间没有任何关系,独立的 * 状态模式各状态之间接口方法都是一样的 * ...

  7. 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)

    说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...

  8. php实现设计模式之 状态模式

    <?php /*状态模式:允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类.(行为模式) * * 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做 ...

  9. Java 策略模式和状态模式

    本文是转载的,转载地址:大白话解释Strategy模式和State模式的区别 先上图: 本质上讲,策略模式和状态模式做得是同一件事:去耦合.怎么去耦合?就是把干什么(语境类)和怎么干(策略接口)分开, ...

  10. javascript - 状态模式 - 简化分支判断流程

    状态模式笔记   当一个对象的内部状态发生改变时,会导致行为的改变,这像是改变了对象   状态模式既是解决程序中臃肿的分支判断语句问题,将每个分支转化为一种状态独立出来,方便每种状态的管理又不至于每次 ...

随机推荐

  1. nodejs中function*、yield和Promise的示例

    var co = require("co"); var fs = require("fs"); function cusReadFile(fileName) { ...

  2. mybatis-generator使用心得

    通过web service给前端返回数据 首先后台先建表, 再针对表进行CRUD的各种sql, 然鹅,现在流行做法是使用mybatis,直接xml把sql融合了,什么事都有利弊,像我这样的手写sql党 ...

  3. 昂达 v891 连接上adb 调试

    这篇文章可以删除了.  可以看看有关v891root和分区的文章,里面介绍了驱动程序的工具了. USB线连上之后,可以直接访问文件,所以连接本身肯定没有问题的. 可是,总是Windows 那边MTP驱 ...

  4. HTML第二章总结

    前言 在第一单元中,我们了解了 "ML":Markup Language,它的作用是: tell the strure of content;在这一单元,进一步认识 HT,它的作用 ...

  5. ssh repo ----> struts+hibernate+spring( jar包和源码)各版本下载链接

    struts http://archive.apache.org/dist/struts/ hibernate http://hibernate.org/orm/releases/5.0/ sprin ...

  6. Sparksql的内置函数的使用以及案例

    开发环境:spark:2.2.0 工具:IDEA OS:Windows 数据文件: 001E8CB5AB11,ASUSTek,2018-07-12 14:00:57,2018-07-12 14:00: ...

  7. BroadcastReceiver(广播)的静态注册和动态注册 --Android开发

    BroadcastReceiver是安卓四大组件之一,本例通过代码的方式演示静态注册和动态注册. 1.静态注册 静态注册只需要AndroidManifest.xml中进行配置: AndroidMani ...

  8. canvas绘图在手机上边缘粗糙

    辛辛苦苦用canvas绘图,做好动画后,想看看效果,结果在手机上一打开,效果是有了,但是边缘特别粗糙,这怎么可以呢,有一种说法是你用canvas绘图时候在手机retain屏幕上,他把一个像素分为两个像 ...

  9. Android的组件化和模块化

    Android随着业务的增多,而且后续新的需求的增加,代码的修改会变得非常频繁 然后最近在看组件化和模块化 公司的业务没有那么大,所以这种方式我并没有采取 但是还是需要了解下他的使用机制 还有优缺点之 ...

  10. kibana的 timelion工具

    时序控件(Timelion)是一款时间序列数据可视化工具,它可以将多种独立的数据源合并呈现到一张视图上. .es函数 index 指明索引    .es(index=nginx-access-log- ...