多维适配器模式所需的正确语法

时间:2017-08-16 15:46:46

标签: java adapter

我想增强适配器模式以允许从多维数组中检索方法。不幸的是,我对正确语法的研究并没有产生类似的例子。我的最终目标是拥有一个三维方法阵列。下面的示例只有2维,但我相信在您的帮助下,我可以将正确的语法推断为其他维度。

package com.js;

public class Node {

    public void goNorth() { System.out.println("Driving North"); }
    public void goSouth() { System.out.println("Driving South"); }
    public void goEast()  { System.out.println("Driving East");  }
    public void goWest()  { System.out.println("Driving West");  }

    interface MoveAction {
        void move();
    }

    private MoveAction[][] moveActions = new MoveAction[][] {
      new MoveAction() { public void move() { goNorth(); } , { public void move() { goSouth(); },
      new MoveAction() { public void move() { goSouth(); } , { public void move() { goNorth(); },
      new MoveAction() { public void move() { goEast();  } , { public void move() { goWest();  },
      new MoveAction() { public void move() { goWest();  } , { public void move() { goEast();  },
    };

    public void move(int index) {
        moveActions[index][index].move();
    }
    public static void main(String[] args) {
        for (int index = 0; index < 4;index++) {
            new Node().moveActions[index][index].move();
        }
    }
}

0 个答案:

没有答案