Java 参数化 class
Java parameterized class
我尝试表示由状态组成的有限状态机。
状态有一个转换列表,一个转换有一个开始状态和一个结束状态。
在我的应用程序中,可以有几种类型的转换,它们都继承自抽象 class Transition.
我在 java 中有那些 class :
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;
}
我认为我有一个设计问题,因为单独设置“过渡”class 似乎很奇怪。
有没有另一种方法可以做到并获得相同的结果?
有人会有不同的做法吗?
感谢您的帮助:)
这是解决同一问题的另一种方法。请注意,关于不同类型的“转换”的详细信息不够多,因此我将一些 assumptions/left 部分空白。指出您希望我更改的内容。
此外,我更改了处理转换的方式。 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
}
}
我尝试表示由状态组成的有限状态机。 状态有一个转换列表,一个转换有一个开始状态和一个结束状态。 在我的应用程序中,可以有几种类型的转换,它们都继承自抽象 class Transition.
我在 java 中有那些 class :
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;
}
我认为我有一个设计问题,因为单独设置“过渡”class 似乎很奇怪。 有没有另一种方法可以做到并获得相同的结果? 有人会有不同的做法吗?
感谢您的帮助:)
这是解决同一问题的另一种方法。请注意,关于不同类型的“转换”的详细信息不够多,因此我将一些 assumptions/left 部分空白。指出您希望我更改的内容。
此外,我更改了处理转换的方式。 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
}
}