我嘗試表示由狀態組成的有限狀態機。狀態有一個轉換串列,一個轉換有一個開始狀態和一個結束狀態。在我的應用程式中,可以有多種型別的轉換,它們都繼承自抽象類 Transition。

我在 java 中有這些類:
public class Etat<T extends Transition<T>> {
private ObservableSet<T> listeTransitions;
}
public abstract class Transition<T extends Transition<T>> {
private Etat<T> etatDepart;
private Etat<T> etatArrivee;
}
我想我有一個設計問題,因為單獨設定我的“過渡”類似乎很奇怪。有沒有另一種方法可以做到并獲得相同的結果?有人會以不同的方式做嗎?
謝謝你的幫助 :)
uj5u.com熱心網友回復:
這是解決同一問題的另一種方法。請注意,關于不同型別的“轉換”沒有足夠的細節,所以我做了一些假設/留了一些空白。指出您希望我更改的任何內容。
此外,我改變了 Transitions 的處理方式。Transitions 不是一個單獨的物件,而是可以通過查看State其他State物件參考哪些物件來找到。
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class State
{
public interface Transition
{
//put whatever methods/functionality/fields/etc that you like here.
}
public enum Type implements Transition
{
A,
B,
;
//If you add any methods to the interface, be sure to implement them in all values of this enum (or give a default/abstract implementation for them to use)
}
private final int id;
private final Map<State, Type> inboundTransitions = new HashMap<>();
private final Map<State, Type> outboundTransitions = new HashMap<>();
public State(int id)
{
this.id = id;
}
public int id()
{
return this.id;
}
public Map<State, Type> inboundTransitions()
{
return Collections.unmodifiableMap(this.inboundTransitions);
}
public Map<State, Type> outboundTransitions()
{
return Collections.unmodifiableMap(this.outboundTransitions);
}
public static void addTransition(State startingState, State endingState, Type transitionType)
{
startingState.outboundTransitions.put(endingState, transitionType);
endingState.inboundTransitions.put(startingState, transitionType);
}
public static void main(String[] args)
{
//now, to show how this would be used, using your image as an example.
State s0 = new State(0);
State s1 = new State(1);
State s2 = new State(2);
State s3 = new State(3);
State.addTransition(s0, s1, Type.B); // s0 ---b---> s1
State.addTransition(s0, s2, Type.A); // s0 ---a---> s2
State.addTransition(s1, s3, Type.A); // s1 ---a---> s3
State.addTransition(s2, s3, Type.A); // s2 ---a---> s3
}
}
如果您使用的是 Java 14 或更高版本,則可以使用Records使事情簡短明了。
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public record State(int id, Map<State, Type> inboundTransitions, Map<State, Type> outboundTransitions)
{
public interface Transition
{
//put whatever methods/functionality/fields/etc that you like here.
}
public enum Type implements Transition
{
A,
B,
;
//If you add any methods to the interface, be sure to implement them in all values of this enum (or give a default/abstract implementation for them to use)
}
public static void addTransition(State startingState, State endingState, Type transitionType)
{
startingState.outboundTransitions.put(endingState, transitionType);
endingState.inboundTransitions.put(startingState, transitionType);
}
public static void main(String[] args)
{
//now, to show how this would be used, using your image as an example.
State s0 = new State(0, new HashMap<>(), new HashMap<>());
State s1 = new State(1, new HashMap<>(), new HashMap<>());
State s2 = new State(2, new HashMap<>(), new HashMap<>());
State s3 = new State(3, new HashMap<>(), new HashMap<>());
State.addTransition(s0, s1, Type.B); // s0 ---b---> s1
State.addTransition(s0, s2, Type.A); // s0 ---a---> s2
State.addTransition(s1, s3, Type.A); // s1 ---a---> s3
State.addTransition(s2, s3, Type.A); // s2 ---a---> s3
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/376253.html
上一篇:使用LINQ過濾串列
下一篇:如何將泛型類轉換為更泛型的型別
