Skip to main content
added 1922 characters in body
Source Link

Implemetation Hints:

you should use a constructor for your Controller that injects the Model

JavaFx Implemetation Hints:

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Pane root;

    @Override
    public void init() {
        Game game = new Game();
        ControllerFactory controllerFactory = new ControllerFactory(game);
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getFxml());
            fxmlLoader.setControllerFactory(controllerFactory);
            root = fxmlLoader.load();
        } catch (IOException e) {
            LOGGER.debug//Log output please ("error",I euse a Logger for that);
        }
        RootController rootController = controllerFactory.getRootController();
        game.init();
        rootController.init();
    }

    @Override
    public void start(Stage stage) {
        if (root != null) {
            stage.setScene(new Scene(root));
            stage.setTitle("title");
            stage.show();
        } else {
            LOGGER.debug//Log output please ("errorI duringuse init"a Logger for that);
            Platform.exit();
            System.exit(0);
        }
    }

}

Implemetation Hints:

you should use a constructor for your Controller that injects the Model

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Pane root;

    @Override
    public void init() {
        Game game = new Game();
        ControllerFactory controllerFactory = new ControllerFactory(game);
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getFxml());
            fxmlLoader.setControllerFactory(controllerFactory);
            root = fxmlLoader.load();
        } catch (IOException e) {
            LOGGER.debug("error", e);
        }
        RootController rootController = controllerFactory.getRootController();
        game.init();
        rootController.init();
    }

    @Override
    public void start(Stage stage) {
        if (root != null) {
            stage.setScene(new Scene(root));
            stage.setTitle("title");
            stage.show();
        } else {
            LOGGER.debug("error during init");
            Platform.exit();
            System.exit(0);
        }
    }

}

JavaFx Implemetation Hints:

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Pane root;

    @Override
    public void init() {
        Game game = new Game();
        ControllerFactory controllerFactory = new ControllerFactory(game);
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getFxml());
            fxmlLoader.setControllerFactory(controllerFactory);
            root = fxmlLoader.load();
        } catch (IOException e) {
            //Log output please (I use a Logger for that)
        }
        RootController rootController = controllerFactory.getRootController();
        game.init();
        rootController.init();
    }

    @Override
    public void start(Stage stage) {
        if (root != null) {
            stage.setScene(new Scene(root));
            stage.setTitle("title");
            stage.show();
        } else {
            //Log output please (I use a Logger for that)
            Platform.exit();
            System.exit(0);
        }
    }

}
added 1922 characters in body
Source Link
public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Pane root;

    @Override
    public void init() {
        Game game = new Game();
Controller controller       ControllerFactory controllerFactory = new ControllerControllerFactory(game);
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getFxml());
            fxmlLoader.setControllerFactory(controllerFactory);
            root = fxmlLoader.load();
        } catch (IOException e) {
            LOGGER.debug("error", e);
        }
        RootController rootController = controllerFactory.getRootController();
        game.init();
        rootController.init();
    }

    @Override
    public void start(Stage stage) {
        if (root != null) {
            stage.setScene(new Scene(root));
            stage.setTitle("title");
            stage.show();
        } else {
            LOGGER.debug("error during init");
            Platform.exit();
            System.exit(0);
        }
    }

}
public class ControllerFactory implements Callback<Class<?>, Object> {

    private final Game game;

    private RootController rootController;

    public ControllerFactory(Game game) {
        this.game = game;
    }

    @Override
    public Object call(Class<?> type) {
        if (type == RootController.class) {
            //inject the Model in the controller
            rootController = new RootController(game); 
            return rootController;
        } else {
            // default behavior for controllerFactory:
            try {
                return type.getDeclaredConstructor().newInstance();
            } catch (Exception exc) {
                exc.printStackTrace();
                throw new RuntimeException(exc); // fatal, just bail...
            }
        }
    }

    public RootController getRootController() {
        return rootController;
    }


}

WhatConclusion What this points out is that the controller is depending on the Game, but the Game is independent from the Controller.

Game game = new Game();
Controller controller = new Controller (game);

What this points out is that the controller is depending on the Game, but the Game is independent from the Controller.

public class App extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Pane root;

    @Override
    public void init() {
        Game game = new Game();
        ControllerFactory controllerFactory = new ControllerFactory(game);
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getFxml());
            fxmlLoader.setControllerFactory(controllerFactory);
            root = fxmlLoader.load();
        } catch (IOException e) {
            LOGGER.debug("error", e);
        }
        RootController rootController = controllerFactory.getRootController();
        game.init();
        rootController.init();
    }

    @Override
    public void start(Stage stage) {
        if (root != null) {
            stage.setScene(new Scene(root));
            stage.setTitle("title");
            stage.show();
        } else {
            LOGGER.debug("error during init");
            Platform.exit();
            System.exit(0);
        }
    }

}
public class ControllerFactory implements Callback<Class<?>, Object> {

    private final Game game;

    private RootController rootController;

    public ControllerFactory(Game game) {
        this.game = game;
    }

    @Override
    public Object call(Class<?> type) {
        if (type == RootController.class) {
            //inject the Model in the controller
            rootController = new RootController(game); 
            return rootController;
        } else {
            // default behavior for controllerFactory:
            try {
                return type.getDeclaredConstructor().newInstance();
            } catch (Exception exc) {
                exc.printStackTrace();
                throw new RuntimeException(exc); // fatal, just bail...
            }
        }
    }

    public RootController getRootController() {
        return rootController;
    }


}

Conclusion What this points out is that the controller is depending on the Game, but the Game is independent from the Controller.

Source Link

if you want to implement a minimal M-VC you have to structure your application kind a like this:

example Diagramm

(This is only an example Diagramm)

Model

The Game is your Model. It stores information about who's turn is it now or how many cards are on this stack (or on that stack). It also provides minimal methods to change these values, e.g. endTurn() turnes the next player into the current player or drawCard() takes a crad from the open stack and adds it into the players hand. It also provides Methods to get the Status of the Model, maybe a method getActivePlayer() will return the current Player, or maybe a method getOpenStack will give you a List of all the Cards form the open Stack of your Game.

IMPORTANT if you create a Model there is NO REFERENCE to the Controller allowed - believe me, this will help you in any further activities on your work. The Model should be a standalone!

Controller (With View)

The Controller is responsible for Drawing the Game and Handle Input - as you mentioned in your question, they have often similar responsibilities and resources.

The Controller in your case should be responsible for creating a GUI with Canvas and Buttons. Maybe it has also an animationThread that frequently updates the screen (for animations and other fancy stuff).

The second responsibility of the Controller would be to handle User Input. The Controller provides for example a button named @FXML Button endTurn;. Here is the controller responsible to grab the input from the User (mostly by attaching a Listener on that Button) and execute the method endTurn on the model side. The controller would also be responsible to execute an Update on the GUI after the button has been pressed, so that the current Player name would be displayed properly. Again, the value of the current Player would com from the model and the Controller would ask the model for the value (by calling the method getCurrentPlayer()on the model.

Implemetation Hints:

you should use a constructor for your Controller that injects the Model

Game game = new Game();
Controller controller = new Controller (game);

What this points out is that the controller is depending on the Game, but the Game is independent from the Controller.

Think of it: you can create new Controllers for any GUI: Android, Console, JavaFX, Swing, TCP/IP, whatever ^_^ - all without ever changing the Game-class!