State mode(State pattern) is a type of design pattern that belongs to the behavior pattern.
definition(from Design Pattern): When an object's internal state changes and agrees to change its behavior, the object looks like it has changed its class.
State mode mainly solves the situation when the conditional expression that controls an object state is too complex.
It is possible to simplify the complex inference logic by transferring the inference logic of state to a series of classes that represent different states.Intentions: Agreeing to change the behavior of an object as it changes its internal stateApplicable scenarios:1. The behavior of an object depends on its state, and it must change its behavior according to state at the time of execution. 2. A single operation contains a large multi-branched structure. And these branches are determined by the state of the object. Headfirst State mode Implementation (C + + writing) This pattern must be in. h files (used to be, feeling a little trouble)
The following is the code section 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::d ispense ()
{
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<< "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::d ispense ()
{
cout<< "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 ();//Invest Money
void Ejectquarter ();//Refund
void Turncrank ();//Swivel crank ...
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::d ispense ()
{
cout<< "No gumball dispened" <<endl;
}
void Soldoutstate::ejectquarter ()
{
cout<< "You can ' t eject,you has ' 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 is 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 ();//Invest Money
void Ejectquarter ();//Refund
void Turncrank ();//Swivel crank ...
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::d ispense ()
{
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<< "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 ();//Invest Money
void Ejectquarter ();//Refund
void Turncrank ();//Swivel crank ...
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;//cast money
virtual void Ejectquarter () =0;//refund
virtual void Turncrank () =0;//rotation crank ...
virtual void dispense () =0;//release sweets ...
};
#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;
} Execution Results
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
National Model C + +