1,状态模式:
控制类中包含所有状态类;
状态类基于一个基类;
每个状态类又有一个控制类成员;
控制类实现每个动作,每个动作内部引起状态的转移.
2,实例代码:
#include <iostream>
#include <memory>
#include <cassert>
#include <ctime>
#include <sstream>
using namespace std;
class State //所有状态类的基类
{
public:
virtual ~State() = 0;
virtual void insertQuarter() const = 0;
virtual void ejectQuarter() const = 0;
virtual void turnCrank() const = 0;
virtual void dispense() = 0;
virtual string toString() const = 0;
};
State::~State() {}
//注意:你中有我,我中有你
class GumballMachine
{
public:
explicit GumballMachine(int numberGumballs );
~GumballMachine()
{
delete _soldOutState;
delete _noQuarterState;
delete _hasQuarterState;
delete _soldState;
delete _winnerState;
}
void insertQuarter() const
{
_state->insertQuarter();
}
void ejectQuarter() const
{
_state->ejectQuarter();
}
void turnCrank() const
{
_state->turnCrank();
_state->dispense();
}
void setState( State* state ) //这个方法的纽带作用
{
assert( state );
_state = state;
}
void releaseBall()
{
cout << "A gumball comes rolling out the slot..." << endl;
if( _count != 0)
{
_count--;
}
}
int getCount() const
{
return _count;
}
void refill( int count )
{
assert( count > 0 );
_count = count;
_state = _noQuarterState;
}
State* getState() const {
return _state;
}
State* getSoldOutState() const
{
return _soldOutState;
}
State* getNoQuarterState() const
{
return _noQuarterState;
}
State* getHasQuarterState() const
{
return _hasQuarterState;
}
State* getSoldState() const
{
return _soldState;
}
State* getWinnerState() const
{
return _winnerState;
}
string toString() const
{
stringstream value;
value << endl << "Mighty Gumball, Inc.";
value << endl << "C++-enabled Standing Gumball Model #2004";
value << endl << "Inventory: " << _count << " gumball";
if( _count > 1 )
{
value << "s";
}
value << endl;
value << "Machine is " << _state->toString() << endl;
return value.str();
}
private:
GumballMachine( const GumballMachine& ); // Disable copy constructor
void operator=( const GumballMachine& ); // Disable assignment operator
private:
State* _soldOutState;
State* _noQuarterState;
State* _hasQuarterState;
State* _soldState;
State* _winnerState;
State* _state;
int _count;
};
class SoldOutState : public State
{
public:
explicit SoldOutState(GumballMachine* gumballMachine ) :
_gumballMachine( gumballMachine )
{
assert(gumballMachine );
}
void insertQuarter() const
{
cout << "You can't insert a quarter, the machine is sold out" << endl;
}
void ejectQuarter() const
{
cout << "You can't eject, you haven't inserted a quarter yet" << endl;
}
void turnCrank() const
{
cout << "You turned, but there are no gumballs" << endl;
}
void dispense()
{
cout << "No gumball dispensed" << endl;
}
string toString() const
{
return "sold out";
}
private:
SoldOutState( const SoldOutState& ); // Disable copy constructor
void operator=( const SoldOutState& ); // Disable assignment operator
private:
GumballMachine* _gumballMachine;
};
class NoQuarterState : public State
{
public:
explicit NoQuarterState( GumballMachine* gumballMachine ) :
_gumballMachine( gumballMachine )
{
assert( gumballMachine );
}
void insertQuarter() const
{
cout << "You inserted a quarter" << endl;
//这里状态转移
_gumballMachine->setState( _gumballMachine->getHasQuarterState() );
}
void ejectQuarter() const
{
cout << "You haven't inserted a quarter" << endl;
}
void turnCrank() const
{
cout << "You turned, but there's no quarter" << endl;
}
void dispense()
{
cout << "You need to pay first" << endl;
}
string toString() const
{
return "waiting for quarter";
}
private:
GumballMachine* _gumballMachine;
private:
NoQuarterState( const NoQuarterState& ); // Disable copy constructor
void operator=( const NoQuarterState& ); // Disable assignment operator
};
class HasQuarterState : public State
{
public:
explicit HasQuarterState( GumballMachine* gumballMachine ) :
_gumballMachine( gumballMachine ), _random( 0 )
{
assert( gumballMachine );
//srand(time(0));
}
void insertQuarter() const
{
cout << "You can't insert another quarter" << endl;
}
void ejectQuarter() const
{
cout << "Quarter returned" << endl;
_gumballMachine->setState( _gumballMachine->getNoQuarterState() );
}
void turnCrank() const
{
cout << "You turned..." << endl;
int winner = 2;//rand() % 5;
if( ( winner == 0 ) && ( _gumballMachine->getCount() > 0 ) )
{
_gumballMachine->setState( _gumballMachine->getWinnerState() );
}
else
{
_gumballMachine->setState( _gumballMachine->getSoldState() );
}
}
void dispense()
{
cout << "No gumball dispensed" << endl;
}
string toString() const
{
return "waiting for turn of crank";
}
private:
HasQuarterState( const HasQuarterState& ); // Disable copy constructor
void operator=( const HasQuarterState& ); // Disable assignment operator
private:
GumballMachine* _gumballMachine;
int _random;
};
class WinnerState : public State
{
private:
GumballMachine* _gumballMachine;
private:
WinnerState( const WinnerState& ); // Disable copy constructor
void operator=( const WinnerState& ); // Disable assignment operator
public:
explicit WinnerState( GumballMachine* gumballMachine ) :
_gumballMachine( gumballMachine )
{
assert( gumballMachine );
}
void insertQuarter() const
{
cout << "Please wait, we're already giving you a Gumball" << endl;
}
void ejectQuarter() const {
cout << "Please wait, we're already giving you a Gumball" << endl;
}
void turnCrank() const {
cout << "Turning again doesn't get you another gumball!" << endl;
}
void dispense()
{
cout << "YOU'RE A WINNER! You get two gumballs for your quarter" << endl;
_gumballMachine->releaseBall();
if( _gumballMachine->getCount() == 0 )
{
_gumballMachine->setState( _gumballMachine->getSoldOutState() );
}
else
{
_gumballMachine->releaseBall();
if( _gumballMachine->getCount() > 0 )
{
_gumballMachine->setState( _gumballMachine->getNoQuarterState() );
}
else
{
cout << "Oops, out of gumballs!" << endl;
_gumballMachine->setState( _gumballMachine->getSoldOutState() );
}
}
}
string toString() const
{
return "despensing two gumballs for your quarter, because YOU'RE A WINNER!";
}
};
class SoldState : public State
{
public:
explicit SoldState( GumballMachine* gumballMachine ) :
_gumballMachine( gumballMachine )
{
assert( gumballMachine );
}
void insertQuarter() const
{
cout << "Please wait, we're already giving you a gumball" << endl;
}
void ejectQuarter() const
{
cout << "Sorry, you already turned the crank" << endl;
}
void turnCrank() const {
cout << "Turning twice doesn't get you another gumball!" << endl;
}
void dispense()
{
_gumballMachine->releaseBall();
if( _gumballMachine->getCount() > 0)
{
_gumballMachine->setState( _gumballMachine->getNoQuarterState() );
}
else
{
cout << "Oops, out of gumballs!" << endl;
_gumballMachine->setState( _gumballMachine->getSoldOutState() );
}
}
string toString() const
{
return "dispensing a gumball";
}
private:
SoldState( const SoldState& ); // Disable copy constructor
void operator=( const SoldState& ); // Disable assignment operator
private:
GumballMachine* _gumballMachine;
};
GumballMachine::GumballMachine(int numberGumballs) :
_count( numberGumballs )
{
assert( numberGumballs >= 0 );
_soldOutState = new SoldOutState( this );
_noQuarterState = new NoQuarterState( this );
_hasQuarterState = new HasQuarterState( this );
_soldState = new SoldState( this );
_winnerState = new WinnerState( this );
_state = _soldOutState;
if( _count > 0 )
{
_state = _noQuarterState;
}
}
int main()
{
auto_ptr< GumballMachine > gumballMachine( new GumballMachine( 10 ) );
cout << gumballMachine->toString() << endl;
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
cout << gumballMachine->toString() << endl;
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
cout << gumballMachine->toString() << endl;
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
cout << gumballMachine->toString() << endl;
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
cout << gumballMachine->toString() << endl;
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
gumballMachine->insertQuarter();
gumballMachine->turnCrank();
cout << gumballMachine->toString() << endl;
return 0;
}
分享到:
相关推荐
State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif else 进行状态切换, 如果针对状态的这样判断切换反复出现,我们就要联想到是否可以采取State模式了. 不只是根据状态,也有根据属性.如果...
State模式是一种行为设计模式,它允许对象在内部状态改变时改变其行为,看起来像是改变了它的类。这种模式常被用于处理具有多种状态的对象,使得对象的状态转换更加清晰,代码更易于理解和维护。 在State模式中,...
State模式是一种行为设计模式,它允许对象在内部状态改变时改变其行为,对象看起来似乎修改了它的类。这种模式常用于处理对象的状态转换,使得代码更易于理解和维护。 State模式的核心概念是状态对象,每个状态对象...
State模式是设计模式中的一种行为模式,它允许对象在内部状态改变时改变其行为,对象看起来好像修改了它的类。这种模式将一个对象的状态抽象出来,封装成独立的类,使得对象可以在不同的状态下表现出不同的行为,而...
Android中StateMachine机制 对与State改变切换这种常见常用的处理, 只是各个平台框架中处理的方法不同, ...在Android中使用的了StateMachine机制就是一个State模式的应用, StateMachine是非常的强大和精妙。
3. **复杂度考量**:虽然State模式能够清晰地表示状态转换,但如果状态较少且转换逻辑简单,则可能不需要使用State模式,因为引入额外的类可能会增加不必要的复杂度。 #### 四、State模式的实例解析 本节将以自动...
Java 设计模式之 State 模式 State 模式是一种行为型设计模式,它允许对象在其内部状态改变时改变其行为。该模式的主要思想是将对象的状态和行为分离,使用不同的状态子类来实现不同的行为。 何时使用 State 模式...
Java 设计模式之 State 模式详解 State 模式是 Java 设计模式的一种,用于解决状态切换问题。当对象的状态改变时,对象的行为也随之改变。State 模式将状态和行为分离,实现了状态切换的灵活性和可扩展性。 定义:...
标题和描述均提到了"C#面向对象设计模式"中的State状态模式,这表明文章的核心内容是围绕State模式在C#编程语言中的应用展开。State模式是一种行为设计模式,旨在允许对象在其内部状态改变时,其行为也能相应地改变...
Java设计模式中的State模式是一种行为设计模式,它允许对象在其内部状态改变时改变它的行为。State模式的核心思想是将状态相关的代码封装到独立的对象中,这样可以使得对象在其状态改变时,表现出不同的行为,而无需...
State模式是一种行为设计模式,它的主要目的是将状态与行为封装在一起,使得对象在状态改变时,其行为也随之改变。在Java中,State模式通常通过创建一系列的子类来表示不同的状态,并让每个子类实现特定状态下的行为...
Java设计模式中的State模式是一种行为设计模式,它允许对象在其内部状态改变时改变它的行为。State模式的核心思想是封装可能改变的行为到相应的状态对象中,让对象在其内部状态改变时可以调用不同的行为方法,而不是...
State模式的核心思想是将一个对象的状态行为封装在独立的类中,这样可以使得对象在不改变其类的情况下,通过改变内部状态来改变其行为。这种模式通常用于当一个对象的行为基于它的状态并且状态变化时,行为也相应地...
状态模式是一种行为设计模式,它允许对象在内部状态改变时改变其行为,使对象看起来好像修改了它的类。在Java中,状态模式通常用于将状态相关的逻辑从对象主体中分离出来,使得对象的行为可以根据其状态动态地更新。...
State模式允许一个对象在其内部状态改变时改变它的行为,对象看起来像是改变了它的类。 #### Observer模式 Observer模式定义了对象之间的一种一对多依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象...
- State模式:允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类。 - Observer模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,所有依赖于它的对象都会得到通知并被自动...