枚举抽象问题 [英] Enum abstraction problem

查看:34
本文介绍了枚举抽象问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力解决 Java 抽象问题.我有这样的事情:

I am currently struggling with a java abstraction problem. I have something like this:

public interface State {
};

public interface Dynamics {
  getObservationChance(State state, Observation observation);
};

class SpecialState implements State {
};

enum SpecialObservation() {
  FREE, WALL, etc.
}

class SpecialDynamics implements Dynamics {
   getObservationChance(State state, Observation observation) {
       // state should be SpecialState, observation should be SpecialObservation!
   }
};

class Main {
  Main(State state, Observation observation, Dynamics dynamics) {
      dynamics.getObservationChance(state, observation);
   }
};

SecialObservation 应该是可能观察到的枚举(或类似的东西),但我想对问题进行抽象表示.所以我想要一个 Observation ,它应该包含观察 一个返回所有可能观察列表的函数.最后一件事对于我正在实施的算法非常重要,因为我必须总结所有可能的观察结果.

SecialObservation should be an enum (or something like that) of possible observations, but I want to have an abstract representation of the problem. So I want an Observation that should contain the observation and a function that returns a list of all possible observations. The last thing is very important for an algorithm I am implementing, since I have to summarize over all possible observations.

谢谢!

推荐答案

这里需要参数化类型 - 每个树类型家族:状态、观察和动态.

You need parametrized types here - you have families of tree types each: a state, a observation, and a dynamics.

如果我们将观察枚举作为参数类型,我们可以将您的类型转换为这样的:

If we take the observation enum as the parameter type, we could convert your type to something like this:

public interface Observation<O extends Observation<O>> {
    ...
}

public interface State<O extends Observation<O>> {
}

public interface Dynamics<O extends Observation<O>> {
  getObservationChance(State<O> state, O observation);
}

enum SpecialObservation implements Observation<SpecialObservation> {
  FREE, WALL, etc.
}

class SpecialState implements State<SpecialObservation> {
}


class SpecialDynamics implements Dynamics<SpecialObservation> {
   getObservationChance(State<SpecialObservation> state, SpecialObservation observation) {
       // state should be SpecialState, observation should be SpecialObservation!
   }
}

class Main<O extends Observation> {
  Main(State<O> state, O observation, Dynamics<O> dynamics) {
      dynamics.getObservationChance(state, observation);
   }
}

当然,只有当我们的 State 接口的方法足以用于 getObservationChance 方法时,这种方法才有效.

This approach only works if the methods of our State interface are enough for the getObservationChance method, of course.

更通用的方法是对所有三种类型进行参数化:

A more general approach would be to parametrize over all three types:

public interface Observation<O extends Observation<O, S, D>,
                             S extends State<O,S,D>,
                             D extends Dynamics<O,S,D>>
{
    ...
}

public interface State<O extends Observation<O,S,D>,
                       S extends State<O,S,D>,
                       D extends Dynamics<O,S,D>> {
}

public interface Dynamics<O extends Observation<O,S,D>,
                          S extends State<O,S,D>,
                          D extends Dynamics<O,S,D>> {
  getObservationChance(S state, O observation);
}

然后我们可以这样定义实现:

Then we can define the implementations as this:

enum SpecialObservation implements Observation<SpecialObservation, SpecialState, SpecialDynamics> {
  FREE, WALL, etc.
}

class SpecialState implements State<SpecialObservation, SpecialState, SpecialDynamics> {
}


class SpecialDynamics implements Dynamics<SpecialObservation, SpecialState, SpecialDynamics> {
   getObservationChance(SpecialObservation state, SpecialObservation observation) {
       // state should be SpecialState, observation should be SpecialObservation!
   }
}

当然,主类需要所有三个参数:

The main class then needs all three parameters, of course:

class Main<O extends Observation<O,S,D>,
           S extends State<O,S,D>,
           D extends Dynamics<O,S,D>> {
  Main(S state, O observation, D dynamics) {
      dynamics.getObservationChance(state, observation);
   }
}

实际上,在您的情况下,动态仅取决于观察和状态,而不是相反(并且这些并不相互依赖),因此另一种方式是:

In your case in fact the dynamics is only dependent on the observation and state, and not the other way around (and these are not dependent on each other), so another way would be this:

public interface Observation {
    ...
}

public interface State {
}

public interface Dynamics<S extends State,
                          O extends Observation> {
  getObservationChance(S state, O observation);
}

enum SpecialObservation implements Observation {
  FREE, WALL, etc.
}

class SpecialState implements State {
}


class SpecialDynamics implements Dynamics<SpecialState, SpecialObservation> {
   getObservationChance(SpecialState state, SpecialObservation observation) {
       // state should be SpecialState, observation should be SpecialObservation!
   }
}

class Main<S extends State, O extends Observation> {
  Main(S state, O observation, Dynamics<S, O> dynamics) {
      dynamics.getObservationChance(state, observation);
   }
}

<小时>

关于 getAllObservations 方法:只要你能以某种方式使你的类型参数具体化,这里就没有真正的问题.要访问特定类型的枚举常量列表,您需要访问此类型 - 直接(SpecialObservation.values()),或使用像这里的类对象:


About the getAllObservations method: As long as you somehow can make your type parameters concrete, there is no real problem here. To access the list of enum constants of a specific type, you need access to this type - either directly (SpecialObservation.values()), or with a class object like here:

class Main<S extends State, O extends Observation> {

  public O[] getAllObservations(Class<O> oClass) {
     return oClass.getEnumConstants();
  }

  Main(S state, Dynamics<S, O> dynamics, Class<O> observationClass) {
      O[] observations = getAllObservations(observationClass);
      for(O o : observations) {
         dynamics.getObservationChance(state, observation);
      }
   }
}

(当然,这仅在 O 是枚举类时才有效.)

(This only works if O is an enum class, of course.)

如果你有一个混合列表,它会变得更复杂,而且类型安全地匹配 Dynamics、Action、Observation 和 State 类也不是很容易.

If you have a mixed list, it gets more complicated, and then it is also not really easy to type-safely match the Dynamics, Action, Observation and State classes.

这篇关于枚举抽象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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