Skip to main content
added 511 characters in body
Source Link
General Waters
  • 1.6k
  • 2
  • 21
  • 30

The way I've done this is by defining an interface to the GUI elements that controllers want access from the view.

public interface GameFrameView {
    public JButton getCard();
        
        // Any other gui elements
}

And then my view classes would implement this interface

public class GameFrame extends JPanel implements GameFrameView {
    private JButton card;

    // Everything else about this view...

    @Override
    public JButton getCard() {
        return card;
    }
}

And then my controller classes would only have reference to the view classes through the interface (in this case, GameFrameView), which I normally fulfill via dependency injection. This way, the two are decoupled and you can change either without disrupting the other (as long as the interface doesn't change, or course).

public class HumanPlayerController {
    private GameFrameView gameFrameView;

    // Notice that I'm passing in reference by the interface type, not the concrete view type that implements the interface.
    public HumanPlayerController(GameFrameView gameFrameView) {
        this.gameFrameView = gameFrameView;
    }

    public void addCardButtonListener() {
        gameFrameView.getCard().addActionListener(new ActionListener()/*etc*/);
    }
}

The way I've done this is by defining an interface to the GUI elements that controllers want access from the view.

public interface GameFrameView {
    public JButton getCard();
        
        // Any other gui elements
}

And then my view classes would implement this interface

public class GameFrame extends JPanel implements GameFrameView {
    private JButton card;

    // Everything else about this view...

    @Override
    public JButton getCard() {
        return card;
    }
}

And then my controller classes would only have reference to the view classes through the interface (in this case, GameFrameView), which I normally fulfill via dependency injection. This way, the two are decoupled and you can change either without disrupting the other (as long as the interface doesn't change, or course).

The way I've done this is by defining an interface to the GUI elements that controllers want access from the view.

public interface GameFrameView {
    public JButton getCard();
        
        // Any other gui elements
}

And then my view classes would implement this interface

public class GameFrame extends JPanel implements GameFrameView {
    private JButton card;

    // Everything else about this view...

    @Override
    public JButton getCard() {
        return card;
    }
}

And then my controller classes would only have reference to the view classes through the interface (in this case, GameFrameView), which I normally fulfill via dependency injection. This way, the two are decoupled and you can change either without disrupting the other (as long as the interface doesn't change, or course).

public class HumanPlayerController {
    private GameFrameView gameFrameView;

    // Notice that I'm passing in reference by the interface type, not the concrete view type that implements the interface.
    public HumanPlayerController(GameFrameView gameFrameView) {
        this.gameFrameView = gameFrameView;
    }

    public void addCardButtonListener() {
        gameFrameView.getCard().addActionListener(new ActionListener()/*etc*/);
    }
}
Source Link
General Waters
  • 1.6k
  • 2
  • 21
  • 30

The way I've done this is by defining an interface to the GUI elements that controllers want access from the view.

public interface GameFrameView {
    public JButton getCard();
        
        // Any other gui elements
}

And then my view classes would implement this interface

public class GameFrame extends JPanel implements GameFrameView {
    private JButton card;

    // Everything else about this view...

    @Override
    public JButton getCard() {
        return card;
    }
}

And then my controller classes would only have reference to the view classes through the interface (in this case, GameFrameView), which I normally fulfill via dependency injection. This way, the two are decoupled and you can change either without disrupting the other (as long as the interface doesn't change, or course).