3

我曾经实现过这样的状态机:

class Player
{
public:
    int Run();
    int Jump();
    int Stop();

private:
    class State
    {
    public:
        virtual int Run() = 0;
        virtual int Jump() = 0;
        virtual int Stop() = 0;
    };

    class StandingState : public State
    {
        virtual int Run() { /*...*/ }
        virtual int Jump() { /*...*/ }
        virtual int Stop() { /*...*/ }
    };

    class RunningState : public State
    {
        virtual int Run() { /*...*/ }
        virtual int Jump() { /*...*/ }
        virtual int Stop() { /*...*/ }
    };

    // More states go here!

    std::list<State*> states;
    State* currentState;
};

int Player::Run()
{
    int result = m_currentState->Run();

    // do something with result
}

int Player::Jump()
{
    int result = m_currentState->Jump();

    // do something with result
}

int Player::Stop()
{
    int result = m_currentState->Stop();

    // do something with result
}

我应该认为相当教科书:Player将来自外部的调用委托给它的当前State对象,并对结果做一些事情(可能转换到另一个状态)。本质上,每个状态都知道给定动作如何影响它,但由状态机将各种状态连接在一起。我发现这是一个很好的关注点分离。

但我在这里看到了抽象的可能性。整个系统由State类的接口定义:

  1. 状态机和子状态都实现State
  2. 状态机保存一个指向所有可能State的 s 和当前State
  3. 无论State在状态机上调用什么方法,它都会被无差别地转发到当前状态。

所以,我们完全可以把它变成一个类模板,对吧?看:

template< class StateInterface >
class StateMachine : public StateInterface
{
    // public methods already declared in StateInterface

protected:
    std::list<StateInterface*> states;
    void AddState(StateInterface* state);
    StateInterface* currentState;
};

class PlayerStateInterface
{
public:
    virtual int Run() = 0;
    virtual int Jump() = 0;
    virtual int Stop() = 0;
};

class Player : public StateMachine< PlayerStateInterface >
{
public:    
    virtual int Run() { currentState->Run(); /* do stuff */ }
    virtual int Jump() { currentState->Jump(); /* do stuff */ }
    virtual int Stop() { currentState->Stop(); /* do stuff */ }
};

在以上几点中,这包括了 1 和 2,但是 3 呢?我仍然必须手动将调用委托给具体状态机实现中的当前状态。有没有办法将该功能移至StateMachine模板?当我不知道's 方法的名称或签名时,我能否以某种方式表示,每当调用 的方法时StateInterfaceStateMachine它应该调用相同的方法 on ?currentStateStateInterface

4

1 回答 1

1

如果您正在寻找 , 和 具有不同签名的情况的一般答案RunJumpStop不知道是否有一个好的解决方案。但是,在您的示例中,它们都具有相同的签名,这向我表明以下方法可能有效:

#include <iostream>

class AbstractState
{
public:
    virtual void write1() = 0;
    virtual void write2() = 0;
};

class State1: public AbstractState
{
public:
    virtual void write1() { std::cout << "1-1" << std::endl; }
    virtual void write2() { std::cout << "1-2" << std::endl; }
};

class State2: public AbstractState
{
public:
    virtual void write1() { std::cout << "2-1" << std::endl; }
    virtual void write2() { std::cout << "2-2" << std::endl; }
};

template <typename StateInterface>
class Player
{
public:
    Player(StateInterface *s_):
        s(s_)
    {
    }

    void setState(StateInterface *s_)
    {
        s = s_;
    }

    void execute(void (StateInterface::*method)())
    {
        (s->*method)();
    }
private:
    StateInterface *s;
};

int main()
{
    State1 s1;
    State2 s2;

    Player<AbstractState> p(&s1);

    p.execute(&AbstractState::write1);
    p.execute(&AbstractState::write2);

    p.setState(&s2);

    p.execute(&AbstractState::write1);
    p.execute(&AbstractState::write2);

    return 0;
}

我能够使用 GCC 4.5.2 编译和运行它并得到预期的结果,即:

1-1
1-2
2-1
2-2

正如我所说,我不确定是否有一种好方法可以将其扩展到不同成员函数AbstractState采用不同参数或返回不同值的情况,并且可能还有其他我尚未考虑的缺点。它不如我认为您希望找到的那么好,但希望这至少可以作为一个好的起点。

于 2013-10-05T00:41:36.287 回答