How to pass native event handler for interrupt in state pattern code

↘锁芯ラ 提交于 2019-12-08 11:26:26

问题


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!


回答1:


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?



来源:https://stackoverflow.com/questions/22690101/how-to-pass-native-event-handler-for-interrupt-in-state-pattern-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!