gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:52:38 27 4
gpt4 key购买 nike

我即将使用状态模式在 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

所以我想知道,我的实现效果好吗?怎么了?我还尝试查看使用复合模式的建议,但我不明白它应该如何与状态模式一起使用。希望大家帮忙!

最佳答案

我认为您也需要 Composite;这将使您能够将状态机链接在一起。

关于c# - 使用状态模式的分层状态机的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3487202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com