Model-View-Controller - Part 4

The Controller is a very important part of the MVC. It controls the updating of both view and model, controlling access to them and confirming they show the same thing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CalculatorController implements  ActionListener
{
    CalculatorModel model;
    CalculatorView_2 view;

    public CalculatorController(CalculatorModel model, CalculatorView_2 view)
    {
        this.model = model;
        this.view = view;

        // Give the model a start situation as per what the GUI will show.
        model.setAnswer(0.0);
        model.setInitialNumber(0.0);

        // Add the action listener from this class on to the buttons of the view.
        view.buttonActionListeners(this);
    }

    // This deals with the interactions performed on the View.
    // It covers addition, subtraction, division and multiplication.
    public void actionPerformed(ActionEvent ae)
    {
        String action_com = ae.getActionCommand();

        if(action_com.equals("+"))
        {
            model.doAddition(view.getFieldText());
        }
        else if (action_com.equals("-"))
        {
            model.doSubtraction(view.getFieldText());
        }
        else if (action_com.equals("*"))
        {
            model.doMultiply(view.getFieldText());
        }
        else if (action_com.equals("/"))
        {
            model.doDivision(view.getFieldText());
        }

        view.setFieldText(""+model.getAnswer());
In the constructor, it takes both model and view. The model is given starting data, and the view is given an action listener for its buttons.
9
10
11
12
13
14
15
16
17
18
19
20
    public CalculatorController(CalculatorModel model, CalculatorView_2 view)
    {
        this.model = model;
        this.view = view;

        // Give the model a start situation as per what the GUI will show.
        model.setAnswer(0.0);
        model.setInitialNumber(0.0);

        // Add the action listener from this class on to the buttons of the view.
        view.buttonActionListeners(this);
    }
The actionPerformed method for the ActionListener is then implemented. Remember how we set a string to button? Now we can set an action to be carried out on the occurrence of a certain string. Looking at an addition, the model is updated due to a user interaction...
26
27
28
29
        String action_com = ae.getActionCommand();

        if(action_com.equals("+"))
        {
...then the view is updated due to the model change.
45
        view.setFieldText(""+model.getAnswer());
Simple, but so powerful if done correctly.

Now we create a class called Calculator that contains the main method. This main method creates a Model, a View and passes it to a Controller. It brings the entire MVC structure together in a three step process that is easy to understand.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Calculator
{
    public static void main(String[] args){

        // Creates a model of the system logic.
        CalculatorModel model = new CalculatorModel();

        // Creaties a view for the system logic.
        CalculatorView view = new CalculatorView();

        // Creates a controller that links the two.
        CalculatorController controller = new CalculatorController(model, view);
    }
}
Eep...I hear the boss coming....!
Back Top Next
Email Me
Code Style


Required Lessons

External Links

Created and Edited by Stuart Davidson
All Rights Reserved ©

Valid XHTML 1.0 Strict