如何通过本地事件处理在状态模式码中断 [英] How to pass native event handler for interrupt in state pattern code

查看:206
本文介绍了如何通过本地事件处理在状态模式码中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转身状态模式我NETMF项目。基于这样的东西: http://www.dofactory.com/Patterns/PatternState.aspx# _self2

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
        }

那么,什么是代码的正确方法有个人OnRotationEventHandlers每个状态?难道是上下文的一部分?抽象基类的一部分?

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?

感谢您的帮助!

推荐答案

我做了一些更多的研究和这里的解决方案,我已经想出了:

I did some more research and here's the solution I've come up with:

我用状态和模式交替

上下文类:

    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?

这篇关于如何通过本地事件处理在状态模式码中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆