涉及N状态的设计模式问题和它们之间的转换 [英] Design Pattern problem involving N states and transitions between them

查看:198
本文介绍了涉及N状态的设计模式问题和它们之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在手,我没有得到使用哪种设计模式。
问题是这样的:



我必须建立一个具有'N'状态的系统,我的系统必须从任何状态到任何状态其他状态取决于一些条件。
例如:
条件1,从状态1移动到3,状态2从状态1移动到4。



甚至从一个状态到其他状态可以在两个或更多不同的条件下完成。



例如,从状态1到状态3的转换可以在以下情况下完成:

条件1:它的星期天

条件2:下雨

条件3:下雨和星期天

在每个条件下,在国家3的处理可能是不同的。



我希望能清楚地了解问题。请帮助。



非常感谢

解决方案

的有限状态机,但它更好地组合条件,而不是为每个组合创建一个新的条件。我不喜欢维基百科的国家模式的Java示例,因为状态知道在许多场景中没有意义的其他状态。保持跟踪 状态,适用条件状态的转换表有助于处理该问题。



我的两分钱是面向对象的有限状态机。你可以在OO前端进行改进,但是它可以得到想法。

  class Transition {
State从;
设置<条件>条件;
状态;
}

class State {
String state;
}

类条件{
字符串条件;
}

可以使用上述类型构建状态机。没有错误检查,但是如果在某些情况下没有找到下一个状态,您可以抛出异常。

  class StateMachine {
列表<转换>过渡;
状态;

StateMachine(状态开始,列表<转换>转换){
this.current = start;
this.transitions = transitions;
}

void apply(Set< Condition> conditions){
current = getNextState(conditions);
}

状态getNextState(Set< Condition> conditions){
for(Transition transition:transitions){
boolean currentStateMatches = transition.from.equals(current) ;
boolean conditionsMatch = transition.conditions.equals(conditions);
if(currentStateMatches&&条件匹配){
return transition.to;
}
}
返回null;
}
}

测试运行:



编辑:根据您的评论,有更多的转换和新状态:

  State one = new State(one); 
状态二=新状态(两);
州三=新州(三);

条件星期日=新条件(星期日);
条件下雨=新条件(下雨);
条件noSunday =新条件(不是星期日);
条件notRaining =新条件(不下雨);

列表<转换> transitions = new ArrayList< Transition>();
transitions.add(one,new Set(sunday),three);
transitions.add(one,new Set(sunday),two); //< <---无效,不能转到两个和三个
transitions.add(一个,新的Set(下雨),三个);
transitions.add(一,新Set(星期日,下雨),三);
transitions.add(一,新Set(notSunday,notRaining),三);

StateMachine machine = new StateMachine(one,transitions);
System.out.print(machine.current); //one
machine.apply(new Set(sunday,raining));
System.out.print(machine.current); //three

我对一个相当大的项目使用状态机有一个苦涩的经验。问题是复合状态,就像你提到的复合条件(星期日和下雨),技术上可能是复合状态,可以进一步分解成单位状态,这可能是也可能不是你的情况,但仍然值得如果是这样,最好修改经典的有限状态机,并使用一组状态而不是一个状态来表示从状态到状态,如果你的N大,这个将有助于保持理智水平的完整性,将hotmail文件夹与gmail标签进行比较,然后转换表将显示为

 转换(Set<状态> from,Set< Condition>条件,Set< State> to)


I have a problem at hand and I am not getting which design pattern to use. The problem goes as such:

I have to build a system which has 'N' states and my system has to do a transition from any state to any other state depending on some conditions. Ex: On condition 1, movement from State 1 to 3 and on condition 2 from state 1 to 4.

Even the transition from one state to other state can be done on 2 or more different conditions.

For example, transition from State 1 to state 3 can be done when:
condition 1 : "Its a Sunday"
condition 2: "Its Raining"
condition 3: "Its Raining and Sunday"
In each condition, the processing at State 3 can be different.

I hope I was able to understand the problem legibly. Kindly help.

Thanks a lot

解决方案

Its clearly a case of a finite-state-machine but its better to combine the conditions rather than create a new condition for each combination. I didn't like the Java example for State pattern on Wikipedia as states know about other states which wouldn't make sense in a lot of scenarios. A transition table that keeps a track of the from state, applicable condition(s), and to state, helps take care of that problem.

My two cents for an object oriented-finite-state-machine. There are improvements you could do on the OO front, but it gets the idea across.

class Transition {
    State from;
    Set<Condition> conditions;
    State to;
}

class State {
    String state;
}

class Condition {
    String condition;
}

The state machine can be constructed with the above types. There is no error checking, but you could throw an exception or something if no next state is found for some conditions.

class StateMachine {
    List<Transition> transitions;
    State current;

    StateMachine(State start, List<Transition> transitions) {
        this.current = start;
        this.transitions = transitions;
    }

    void apply(Set<Condition> conditions) {
        current = getNextState(conditions);
    }

    State getNextState(Set<Condition> conditions) {
        for(Transition transition : transitions) {
            boolean currentStateMatches = transition.from.equals(current);
            boolean conditionsMatch = transition.conditions.equals(conditions);
            if(currentStateMatches && conditionsMatch) {
                return transition.to;
            }
        }
        return null;
    }
}

And a test run:

Edit: With some more transitions and new states based on your comment:

State one = new State("one");
State two = new State("two");
State three = new State("three");

Condition sunday = new Condition("Sunday");
Condition raining = new Condition("Raining");
Condition notSunday = new Condition("Not Sunday");
Condition notRaining = new Condition("Not Raining");

List<Transition> transitions = new ArrayList<Transition>();
transitions.add(one, new Set(sunday), three);
transitions.add(one, new Set(sunday), two); // <<--- Invalid, cant go to two and three
transitions.add(one, new Set(raining), three);
transitions.add(one, new Set(sunday, raining), three);
transitions.add(one, new Set(notSunday, notRaining), three);

StateMachine machine = new StateMachine(one, transitions);
System.out.print(machine.current); // "one"
machine.apply(new Set(sunday, raining));
System.out.print(machine.current); // "three

I had a bitter experience with using a state machine for a fairly large project. The problem was with composite states. Just like the composite condition you mentioned (sunday and raining), there could technically be composite states which could further be broken down into unit states. This may or may not be the case in your situation, but still worth mentioning. If that is the case, it's best to modify the classical finite state machine and use a set of states instead of a single state to represent the from and to states. If your N is large, this will help keep sanity levels intact. Think hotmail folders vs gmail tags. The transition table would then be presented as

Transition(Set<State> from, Set<Condition> conditions, Set<State> to)

这篇关于涉及N状态的设计模式问题和它们之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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