这是解决相同问题的另一种方法。请注意,关于不同类型的“转换”没有足够的细节,所以我做了一些假设/留下了一些空白。指出您希望我更改的任何内容。
另外,我改变了处理过渡的方式。通过查看哪些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
}
}