使用状态模式的分层状态机的最佳实践是什么?

时间:2010-08-15 11:39:17

标签: c# class nested hierarchical fsm

我即将使用状态模式在C#中实现分层状态机。作为指南我正在使用this示例。但是,该示例并未提供有关分层状态的答案。不幸的是,我似乎无法在其他地方找到好的例子。我的第一个想法是为分层状态创建嵌套类。但这被认为是最佳实践还是有更好的解决方案?

电贺!

更新

我整个下午都在试图实现上述状态模式。 HSM基于一个非常简单的媒体播放器:

alt text http://www.freeimagehosting.net/uploads/e8d2d6486a.jpg

我以为我已经完成了,但有一件事我不明白。首先是我写的代码(对不起,非常多):

public class MediaPlayer
{
    public MediaPlayerStates state;

    public MediaPlayer(MediaPlayerStates state)
    {
        this.state = state;
    }

    public void OnButtonPressed()
    {
        state.OnButtonPressed(this);
    }

    public void DeviceBooted()
    { 
        state. ?????
    }

    //Other Functions
}

//The 3 initial states (Start, On, End) know only 2 events.
public abstract class MediaPlayerStates
{
    public abstract void OnButtonPressed(MediaPlayer player);
    public abstract void OffButtonPressed(MediaPlayer player);
}

//The very beginpoint of the state machine
public class Start : MediaPlayerStates
{
    //When hitting the onbutton, the state changes to the OnState state
    public override void OnButtonPressed(MediaPlayer player)
    {
        player.state = new OnState(player);
    }

    //No need to implement this one
    public override void OffButtonPressed(MediaPlayer player)
    {
        throw new NotImplementedException();
    }
}

//OnState implements the 2 events from the MediaPlayerStates abstract class.
public class OnState : MediaPlayerStates
{
    //When entered the OnState state, a new entrypoint is creaeted: the Start state
    public OnState(MediaPlayer player)
    {
        player.state = new OnStartState();
    }

    //The OnState doesn't have a OnButtonPressed event so it doesn't need to be implemented
    public override void OnButtonPressed(MediaPlayer player)
    {
        throw new NotImplementedException();
    }

    //When hitting the offbutton in the OnState, the new state is End
    public override void OffButtonPressed(MediaPlayer player)
    {
        player.state = new End();
    }

    //The OnState itself containts 3 events, therefore these need to be implemented by every state whitin the OnState state
    public abstract class SubStates : MediaPlayerStates
    {
        public abstract void DeviceBooted(MediaPlayer player);
        public abstract void PlayButtonPressed(MediaPlayer player);
        public abstract void StopButtonPressed(MediaPlayer player);
    }

    //The OnStartState is the pseudoState where the On state starts
    public class OnStartState : SubStates
    {
        //When booted, the state of the player changes to the ShowMediaFileState state
        public override void DeviceBooted(MediaPlayer player)
        {
            player.state = new ShowMediaFileState();
        }

        //The events below don't need to be implemented since they don't exist. 
        public override void PlayButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        public override void StopButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        public override void OnButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        public override void OffButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }
    }

    public class ShowMediaFileState : SubStates
    {
        //This event doesn't exists for this state
        public override void DeviceBooted(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        //When hitting the play button in this state, play the mediafile
        public override void PlayButtonPressed(MediaPlayer player)
        {
            player.state = new PlayMediaFileState();
        }

        //These events also don't exist for this state
        public override void StopButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        public override void OnButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        public override void OffButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }
    }

    public class PlayMediaFileState : SubStates
    {
        //This event doesn't exist for this state
        public override void DeviceBooted(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        //This event doesn't exist for this state
        public override void PlayButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        //While playing a file and hitting the stopbutton, the state changes to the ShowMediaFileState state
        public override void StopButtonPressed(MediaPlayer player)
        {
            player.state = new ShowMediaFileState();
        }

        //This event doesn't exist for this state
        public override void OnButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }

        //This event doesn't exist for this state
        public override void OffButtonPressed(MediaPlayer player)
        {
            throw new NotImplementedException();
        }
    }
}

//The endstate doesn't need any implementation since there cannot occur a event while being off
public class End : MediaPlayerStates
{
    public override void OnButtonPressed(MediaPlayer player)
    {
        throw new NotImplementedException();
    }

    public override void OffButtonPressed(MediaPlayer player)
    {
        throw new NotImplementedException();
    }
}

在MediaPlayer类中定义事件时,我无法调用任何其他函数

  • OnButtonPressed
  • OffButtonPressed

所以我想知道,我的实施是否有益?怎么了?我也试着看一下使用复合模式的建议,但我不明白它应该如何与状态模式一起使用。希望有人可以帮忙!

4 个答案:

答案 0 :(得分:3)

我想你也会想要复合材料;这将允许您将状态机链接在一起。

答案 1 :(得分:2)

为了制作具有状态模式的HSM,具有子状态的每个状态必须是状态机本身。这样,上层不知道子状态(副作用较少),状态可以更好地管理他的子状态(可以有一个默认状态,可以记住它所处的最后状态等)。

当你无法对某个动作做任何有用的事情时BTW抛出异常是错误的。你应该忽略它。您只在异常情况下抛出异常,按下错误按钮是预期的用户行为。

答案 2 :(得分:1)

在开始实现自己的FSM框架之前,请先查看SMC - 状态机编译器。

SMC采用状态机的文本定义并生成实现它的代码。它有适用于各种语言的后端,包括C#。它还可以输出点文件以生成FSM图。

SMC可以通过推送和弹出转换创建类似于分层状态机的东西 - 实际上是将转移控制推送到新的状态机,然后pop将控制返回到原始状态机。

答案 3 :(得分:1)

为了使它以通用方式工作,您需要将状态机的层次结构视为树结构;节点之间的转换可以使用最小公共祖先(LCA)算法用于树,然后从源节点祖先上的公共祖先下方的节点退出(将出口级联到任何子节点),然后从目标节点上输入每个节点祖先在目标节点的共同祖先下面的节点,最后,如果目标节点有子节点,则需要输入这些节点,就像输入任何其他复合状态一样。

这是UML超结构规范中提到的方法。

查看https://github.com/steelbreeze/state.cs中的源代码,因为它实现了上述方法。

要查看一个有效的示例,请在此处查看姐妹JavaScript版本的项目网站:http://www.steelbreeze.net/state.js/