状态模式(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;

}
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



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;

}

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;

}

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

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
#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


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;

}
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
state.cpp
#include "state.h"





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


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;

}
执行结果

版权声明:本文博主原创文章,博客,未经同意不得转载。

国家模式c++的更多相关文章

  1. 大话设计模式C++达到-文章16章-国家模式

    一.UML画画 二.概念 状态模式(State):当一个对象的内在状态改变时同意改变其行为.这个对象看起来像是改变了其类. 三.说明 以下是来自书本和网络的对状态模式的定义和分析: (1)状态模式同意 ...

  2. SAP增强 和VA01相关增强点介绍

    -转 sap寻找用户出口方法 sap的用户出口总共有三代: 一.User EXIT 第一代的用户出口,它们include在SAP标准程序的源代码里,可以说他们是源代码的一部分,你改了这种出口就相当于改 ...

  3. 一点公益商城开发系统模式Ring Buffer+

    一个队列如果只生产不消费肯定不行的,那么如何及时消费Ring Buffer的数据呢?简单的方案就是当Ring Buffer"写满"的时候一次性将数据"消费"掉. ...

  4. 从接口、抽象类到工厂模式再到JVM来总结一些问题

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 涉及到的知识点总结如下: 为什么使用接口? 接口和抽象类的区别 简单工厂模式总结 Java中new和newInstance的区别 J ...

  5. 高访问量WEB开发中的架构模式,学习从点滴开始

     当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构 ...

  6. Java设计模式之工厂模式(Factory)

    前言: 前面学习了建造者设计模式,接下来学习一下Retrofit中使用的另外一个设计模式,工厂设计模式!!!里面采用工厂模式使得数据转换得到完全解耦,工厂模式的好处用到了极致,如此好的设计模式我们怎能 ...

  7. 两种交换机配置模式,以配置基于端口划分的VLAN为例

    关于交换机的配置模式,大体上可以分为两类:其一以CISCO交换机为代表的配置模式,其二以Huawei.H3C交换机为代表的配置模式.其实这两种配置模式并没有本质的不同,只是配置的命令名称和配置方式存在 ...

  8. 在WildFly中运行多个standalone模式的实例

      WildFly作为一款优秀的EJB容器,其前身为JBoss AS.JBoss作为一款开源的应用服务器,被广泛的应用在各种项目当中.假设我们现在有这样一个项目,他是以standalone的模式运行在 ...

  9. [转] ACM中国国家集训队论文集目录(1999-2009)

    国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...

随机推荐

  1. [Angular2 Form] Reactive Form, FormBuilder

    Import module: import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/comm ...

  2. complex query几个原则

    1.一般来说in比exists更有利(更容易变成join). 2.尽量避免union,使用union all代替,避免sort. 3,绝对不能在没有on条件下使用join(除非有特殊目的). 4.ou ...

  3. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  4. Netty+WebSocket简单实现网页聊天

    基于Netty+WebSocket的网页聊天简单实现 一.pom依赖 <dependency>        <groupId>io.netty</groupId> ...

  5. 360随身WIFI作USB无线网卡的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 1. 到控制面板上把360wifi卸载. 2. 到雷凌的官网下载网卡驱动,注意选择USB(RT2870***),操作系 ...

  6. 【u030】扑克牌

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的 ...

  7. Java 之 assert (断言)

    我们知道C/C++语言中有断言的功能(assert).在Java SE 1.4版本以后也增加了断言的特性. 断言是为了方便调试程序,并不是发布程序的组成部分.理解这一点是很关键的. 默认情况下,JVM ...

  8. [React] Recompose: Theme React Components Live with Context

    SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. P ...

  9. 【hdu 3951】Coin Game

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  10. [Angular] @ContentChild and ngAfterContentInit

    @ContentChild normally works with ngAfterContentInit lifecycle. @ContentChild is used for looking in ...