MVC Layout - Which way to add listeners is better? So I'm doing a basic MVC layout for a pretty basic game that I am making. The game requires the user to move up/down/left/right via buttons on the GUI. Since I'm using an MVC layout and my buttons are in a different class than the ActionListeners, I was wondering what the best way to add the action listeners are?

Method 1:

View Class ActionListener method:

public void addMovementListeners(ActionListener u, ActionListener d, ActionListener l, ActionListener r){
    moveUp.addActionListener(u);
    moveDown.addActionListener(d);
    moveLeft.addActionListener(l);
    moveRight.addActionListener(r);
}


Control Class add ActionListener method:

private void addListeners(){
    viewGUI.addMovementListeners(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            buttonPressed = 1;
        }
    },
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            buttonPressed = 2;
        }
    },
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            buttonPressed = 3;
        }
    },
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            buttonPressed = 4;
        }
    });
}




Method 2:

View Class ActionListener method:

public void addMovementListeners(ActionListener a){
    moveUp.addActionListener(a);
    moveDown.addActionListener(a);
    moveLeft.addActionListener(a);
    moveRight.addActionListener(a);
}

public JButton[] getButtons(){
    JButton[] temp = new JButton[4];
    temp[0] = moveUp;
    temp[1] = moveDown;
    temp[2] = moveLeft;
    temp[3] = moveRight;

    return temp;
}


Control Class add ActionListener method:

JButton[] movementButtons;

private void addListeners(){
    movementButtons = viewGUI.getButtons();
    viewGUI.addMovementListeners(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == movementButtons[0]){
                buttonPressed = 1;
            }else if(e.getSource() == movementButtons[1]){
                buttonPressed = 2;
            }else if(e.getSource() == movementButtons[2]){
                buttonPressed = 3;
            }else if(e.getSource() == movementButtons[3]){
                buttonPressed = 4;
            }
        }
    });
}




Which method is better? Is there another method that is even better than these two? Let me know! Trying to get this MVC thing down :). (Any other suggestions welcome as well)!
Method #1 feels like the better approach, because it doesn't require your view to expose its buttons, but the four-times ActionListener method seems odd.  Both methods have some trouble with magic numbers.

Introduce an enum for directions.  It may feel like overkill at the moment, but it'll take care of your direction-to-action mapping.  It will also help shape some of your options.

Then consider supplying Action instead of ActionListener.  This will also make it easier to enable/disable the movement buttons in case something blocks your way (like going off-field), without your view having to expose its buttons.

When you combine these two, you get something like the following:

View:
    void setAction(Direction dir, Action action);

Model:
    void move(Direction dir);
    /** Returns directions possible to move to at this moment. */
    Set&lt;Direction&gt; getPossibleDirections();

enum Direction { UP, RIGHT, DOWN, LEFT; }

class Controller {
  Map&lt;Direction, MoveAction&gt; actions;

  public void setView(View view) {
    actions = new EnumMap&lt;&gt;(Direction.class);
    for ( final Direction direction : Direction.values() ) {
      final MoveAction action = new MoveAction(direction, direction.toString());
      actions.put(direction, action);
      view.setAction(direction, action);
    }
  }

  void refreshState() {
    final Set&lt;Direction&gt; dirs = model.getPossibleDirections();
    for ( final MoveAction action : actions.values() ) {
      action.setEnabled(dirs.contains(action.direction));
    }
  }

  class MoveAction extends AbstractAction {
    final Direction direction;

    MoveAction(Direction direction, String name) {
      super(name);
      this.direction = direction;
    }

    public void actionPerformed(ActionEvent evt) {
      model.move(direction);
      refreshState(); // or have model firePropertyChange
    }
  }
}