【问题标题】:Java parameterized classJava参数化类
【发布时间】:2021-12-01 08:52:53
【问题描述】:

我尝试表示由状态组成的有限状态机。 状态有一个转换列表,一个转换有一个开始状态和一个结束状态。 在我的应用程序中,可以有几种类型的转换,它们都继承自抽象类 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;
}

我认为我有一个设计问题,因为单独设置我的“Transition”类似乎很奇怪。 还有其他方法可以做到这一点并获得相同的结果吗? 有人会做不同的事吗?

感谢您的帮助:)

【问题讨论】:

  • 你想达到什么目的?
  • 不清楚“做”中的“它”是什么。你能解释一下EtatTransition代表什么以及它们之间的关系吗?
  • 我编辑了我的帖子。我希望它更清楚。 @Kostakiiiis
  • 我编辑了我的帖子。我希望它更清楚。 @JoachimSauer

标签: java generics parameterized


【解决方案1】:

这是解决相同问题的另一种方法。请注意,关于不同类型的“转换”没有足够的细节,所以我做了一些假设/留下了一些空白。指出您希望我更改的任何内容。

另外,我改变了处理过渡的方式。通过查看哪些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
   
   }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多