Моя программа анализирует WebAssembly
инструкций и принимает решения на основе контекста текущей инструкции. Итак, MWE для моего алгоритма выглядит следующим образом:
public class Main {
public interface Context {
String name();
}
static class M implements Context {
public String name() {
return "Context M: ";
}
}
static class N implements Context {
public String name() {
return "Context N: ";
}
}
public interface Instruction {
int getId();
String run();
}
static class A implements Instruction {
public int getId() {
return 0;
}
public String run() {
return "The work of A";
}
}
static class B implements Instruction {
public int getId() {
return 1;
}
public String run() {
return "The work of B";
}
}
static void work(Context context, Instruction instruction) {
switch (instruction.getId()) {
case 0:
workOnId0(context, (A) instruction);
break;
case 1:
workOnId1(context, (B) instruction);
break;
default:
throw new RuntimeException("Failed to recognize instruction");
}
}
static void workOnId0(Context context, A instruction) {
System.out.println(context.name() + instruction.run());
}
static void workOnId1(Context context, B instruction) {
System.out.println(context.name() + instruction.run());
}
static void workOnId1(N context, B instruction) {
System.out.println("This is corner case logic for this context!");
}
public static void main(String[] args) {
N context = new N();
B instruction = new B();
work(context, instruction);
}
}
Как вы можете видеть сверху, когда моя инструкция B
, тогда обычная работа должна произойти в workOnId1
, но в случае, если мой в частности, контекст N
, я хотел бы выполнить некоторую специальную работу, которая представлена другой перегрузкой workOnId1
.
К сожалению, специальная перегрузка никогда не вызывается. Как я могу заставить работать разрешение перегрузки?