#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. Windows 下 Redis 服务无法启动,错误 1067 进程意外终止解决方案

    1.检查端口是否被占用 2.修改 Windows 服务里的 Redis 服务为本地系统服务(修改方式见下文) 方法: 1.看系统日志 桌面计算机/此电脑(Win10名称)右键打开管理,或 Win+R ...

  2. 20165303 预备作业3 Linux安装及学习

    =20165303 预备作业3 Linux安装及学习 安装虚拟机 按照http://www.cnblogs.com/rocedu/p/6012545.html步骤进行逐步操作,安装虚拟机. 遇到的问题 ...

  3. 10 Node Frameworks to Use in 2019

    原文 https://scotch.io/bar-talk/10-node-frameworks-to-use-in-2019 node Framworks列表: http://nodeframewo ...

  4. php数组的逐行写入文件与读取

    <?php /** * * 对数组$arr1=['Apple Orange Banana Strawberry'] 写入文件,并读取 **/ class IoFile { private $pa ...

  5. Linear Kingdom Races CodeForces - 115E (线段树优化dp)

    大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...

  6. 4月12 php练习

    php中输出 <?php echo'hello'; php中打印多个div <?php for($i=1;$i<=100;$i++) { ?> <div style=&q ...

  7. vue组件插槽

    vue中子组件内容如何定义为可扩展的呢,就是用slot插槽来实现.如下图 如果<slot></slot>标签有内容,那就默认显示里面的内容,父组件传了就会覆盖此默认的内容.

  8. 创建springboot的聚合工程(三)

    springboot聚合工程之添加mybatis数据库持久化操作 在boot-polymer-repository工程添加mybatis的相关依赖 <project xmlns="ht ...

  9. mysql创建存储过程,批量建表分表00到99

    这里以sqlyong为软件示例: --创建存储过程DELIMITER $$ CREATE PROCEDURE `createTablesWithIndex`() BEGIN DECLARE `@i` ...

  10. python-django rest framework框架之解析器

    1.解析器 : 对请求的数据进行解析 - 请求体进行解析. 解析器在你不拿请求体数据时 不会调用. class UsersView(APIView): def get(self,request,*ar ...