这会被视为滥用状态机模式吗?

时间:2018-08-26 01:24:17

标签: c# oop design-patterns workflow state-machine

我想要某种代表工作流状态的对象,其中工作流会设置某些值,但不是按特定的顺序。我通过使用枚举标志来推进状态,例如

[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;
    }

}

这是反模式吗?滥用状态机模式?滥用旗帜?如果是这样,我要寻找的正确模式是什么?

0 个答案:

没有答案