问题描述
我已经转向了我的netmf项目的状态模式。基于此的内容:
我有一个旋转编码器旋钮,每个状态都会有不同的作用。
我一直在试图围绕着我的头脑,不能在任何地方工作。我不知道在哪里以及如何将中断处理程序注入每个状态,以及如何调用中断处理程序的切换。没有状态模式,代码看起来像:
RotaryEncoder RE = new RotaryEncoder(pin1,pin2); //创建新的实例的旋钮
RE.OnRotationEvent + = OnRotationEventHandler; //订阅一个事件处理程序。
//做其他事情
...
...
static void OnRotationEventHandler(uint data1,uint data2,DateTime time)
{
//做某事
}
那么代码的正确方法是个别OnRotationEventHandlers为每个州?是上下文的一部分吗?抽象基础的一部分?
感谢您的帮助!
我做了更多的研究,我想到:
我可以互换使用状态和模式
上下文类: p>
class ModeContext
{
private int rotationCount;
私人字符串方向;
私人州州;
public RotaryEncoder rotationEncoderInstance;
public ModeContext(RotaryEncoder re)
{
this.State = new RPMMode(this);
rotationEncoderInstance = re;
re.RotationEventHandler + = OnRotationEvent; //附加到Context的Handler
rotationCount = 0;
}
public State State
{
get {return state; }
set {state = value; //调试状态更改
}
//事件处理程序
public void OnRotationEvent(uint data1,uint data2,DateTime time)
{
rotationCount ++ ;
if(data1 == 1)
{
direction =顺时针;
state.OnCWRotationEvent(this);
}
else
{
direction =逆时针;
state.OnCCWRotationEvent(this);
}
Debug.Print(rotationCount.ToString()+:+ direction +上下文模式旋转事件被激活!
}
继承State Base类的具体状态类:
class Mode2:State
{
public override void Handle(ModeContext mode)
{
mode。 State = new Mode2(); //(mode);
}
public Mode2()
{
//做某事;
}
#region事件处理程序
public override void OnCWRotationEvent(ModeContext mode)
{
mode.State = new Mode3(mode);
}
public override void OnCCWRotationEvent(ModeContext mode)
{
mode.State = new Mode1(mode);
}
#endregion
}
现在我可以改变状态并给每个国家具体的控制行为,实际的重物在哪里去?
I've turned to state pattern for my netmf project. Something based on this: http://www.dofactory.com/Patterns/PatternState.aspx#_self2
I have a rotary encoder knob that will act differently in each state.
I've been trying to wrap my head around this and can't get anything to work on my end. I'm not sure where and how to inject the interrupt handler into each state and how to invoke the switch of the interrupt handler. Without the State Pattern the code looks something like:
RotaryEncoder RE = new RotaryEncoder(pin1, pin2);//create new instance of knob
RE.OnRotationEvent += OnRotationEventHandler;//subscribe to an event handler.
//do other things
...
...
static void OnRotationEventHandler(uint data1, uint data2, DateTime time)
{
//do something
}
So, what's the right way to code have individual "OnRotationEventHandlers" for each state? Is it part of the context? Part of the abstract base class?
Thanks for the help!
I did some more research and here's the solution I've come up with:
I use "state" and "mode" interchangeably
Context Class:
class ModeContext
{
private int rotationCount;
private string direction;
private State state;
public RotaryEncoder rotaryEncoderInstance;
public ModeContext( RotaryEncoder re)
{
this.State = new RPMMode(this);
rotaryEncoderInstance = re;
re.RotationEventHandler += OnRotationEvent;//attach to Context's Handler
rotationCount = 0;
}
public State State
{
get { return state; }
set { state = value; }//debug state change
}
//Event Handler
public void OnRotationEvent(uint data1, uint data2, DateTime time)
{
rotationCount++;
if (data1 == 1)
{
direction = "Clockwise";
state.OnCWRotationEvent(this);
}
else
{
direction = "Counter-Clockwise";
state.OnCCWRotationEvent(this);
}
Debug.Print(rotationCount.ToString() + ": " + direction + " Context Mode Rotation Event Fired!");
}
Concrete State Class that Inherits The State Base class:
class Mode2 : State
{
public override void Handle(ModeContext mode)
{
mode.State = new Mode2();//(mode);
}
public Mode2()
{
//do something;
}
#region event handlers
public override void OnCWRotationEvent(ModeContext mode)
{
mode.State = new Mode3(mode);
}
public override void OnCCWRotationEvent(ModeContext mode)
{
mode.State = new Mode1(mode);
}
#endregion
}
Now that I can change state and give each state specific control behavior, where does the actual heavy lifting go?
这篇关于如何在状态模式代码中传递中断的本机事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!