samedi 25 août 2018

Would this be considered an abuse of the State Machine Pattern?

I want some type of object that represents a workflow state, where the workflow sets certain values but not in a certain order. I advance the state by using enum flags, like

[Flags]
public enum MyState
{
    None = 0,
    FooSet = 1,
    BarSet = 2
}

public class MyStateMachine
{
    public MyState State { get; private set; }

    public bool Completed
    {
        get
        {
            return State == Enum.GetValues(typeof(MyState))
                .Cast<MyState>()
                .Aggregate(MyState.None, (prev, cur) => prev | cur);
        }
    }

    public string Foo
    {
        get
        {
            return foo; 
        }
        set
        {
            foo = value;
            State |= MyState.FooSet;
        }
    }

    private string foo;

    public string Bar
    {
        get
        {
            return bar; 
        }
        set
        {
            bar = value;
            State |= MyState.BarSet;
        }       
    }

    private string bar;

    public MyStateMachine()
    {
        State = MyState.None;
        foo = null;
        bar = null;
    }

}

Is this an anti-pattern? An abuse of the State Machine Pattern? An abuse of flags? If so, what is the correct pattern I'm looking for?

Aucun commentaire:

Enregistrer un commentaire