I have several questions on mvc at once. I will ask on the example of a toy program, which I specifically wrote for this:
public class View implements Observer { private Model model; private IController controller; private JLabel firstNumber = new JLabel(); private JLabel secondNumber = new JLabel(); private JLabel resultLabel = new JLabel(); public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Model model = new Model(); IController controller = new Controller(); View view = new View(); view.setModel(model); view.setController(controller); view.createAndShowGUI(); } }); } public void setModel(Model model) { this.model = model; model.addObserver(this); } public void setController(IController controller) { this.controller = controller; } public void createAndShowGUI() { JFrame frame = new JFrame("mvc train"); frame.setSize(400, 400); frame.getContentPane().add(createMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private Component createMainPanel() { JPanel mainPanel = new JPanel(); mainPanel.add(firstNumber); mainPanel.add(secondNumber); mainPanel.add(resultLabel); JButton inc1 = new JButton("increment first"); inc1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { controller.execute("inc1", model); } }); JButton dec1 = new JButton("decrement first"); dec1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { controller.execute("dec1", model); } }); mainPanel.add(inc1); mainPanel.add(dec1); JButton inc2 = new JButton("increment second"); inc2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { controller.execute("inc2", model); } }); JButton dec2 = new JButton("decrement second"); dec2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { controller.execute("dec2", model); } }); mainPanel.add(inc2); mainPanel.add(dec2); return mainPanel; } @Override public void update(Observable arg0, Object arg1) { firstNumber.setText("" + model.getA()); secondNumber.setText("" + model.getB()); resultLabel.setText("" + (model.getA() + model.getB())); } } This is a presentation class. It turns out this picture: 
The increment first button increments the first number, increment second - the second, decrement first decrements the first number by one, decrement secomd - the second. In general, after two clicks on the increment first and one click on the increment second we get the following picture:
Two numbers are stored in the model. Here she is:
public class Model extends Observable { private int a; private int b; public void incrementA() { a++; setChanged(); notifyObservers(); } public void incrementB() { b++; setChanged(); notifyObservers(); } public void decrementA() { a--; setChanged(); notifyObservers(); } public void decrementB() { b--; setChanged(); notifyObservers(); } public int getA() { return a; } public int getB() { return b; } @Override public void addObserver(Observer observer) { super.addObserver(observer); setChanged(); notifyObservers(); } } My controller:
public class Controller implements IController { @Override public void execute(String action, Model model) { switch (action) { case "inc1": model.incrementA(); break; case "inc2": model.incrementB(); break; case "dec1": model.decrementA(); break; case "dec2": model.decrementB(); } } } Now questions. The main question is actually in the title.
- How did the controller help us? The code from the case branches of the switch statement in the controller might as well be in the listeners. For each branch on the listener. As a result, listeners still have to be created to call the controller method. And then in this method in the switch statement to prescribe the same code that could be in the listeners. Maybe I generally implemented mvc incorrectly? I suspect that this is so. And it is in the part of the controller. Because there is little information on it in the network. If this is the case, please suggest how it is correct. I heard that you can create controllers for each element (in this case for each button). But the situation does not change much.
- In my example, all the methods that are called in the case branches have no parameters. In real programs, this is likely to be wrong. Some buttons could require method calls with parameters, and with a different number of parameters. What to do in this situation?
- Where it is better to calculate the rightmost number? In my example, I do this in the update () function. Isn't it better to do this in a model?
