Graphical User Interfaces: Calculator Application Controller
Download
Report
Transcript Graphical User Interfaces: Calculator Application Controller
Controller
controller
processes and responds to events (such as user actions)
from the view and translates them to model method calls
needs to interact with both the view and the model
but does not own the view or model
aggregation
View is a
subclass
of JFrame
JFrame
View
1
Controller
Controller has
1 View
1
1
Model
Controller has
1 Model
CalcController: Attributes & Constructor
import java.awt.event.ActionListener;
public class CalcController implements ActionListener
{
private CalcModel model;
private CalcView view;
public CalcController(CalcModel model, CalcView view)
{
this.model = model;
this.view = view;
}
}
2
CalcController
recall that our application only uses events that are
fired by buttons (JButtons and JMenuItems)
a button fires an ActionEvent event whenever it is
clicked
CalcController listens for fired ActionEvents
how? by implementing the ActionListener interface
public interface ActionListener
{
void actionPerformed(ActionEvent e);
}
3
CalcController was registered to listen for
ActionEvents fired by the various buttons in
CalcView (see method setCommand in CalcView)
whenever a button fires an event, it passes an
ActionEvent object to CalcController via the
actionPerformed method
4
actionPerformed is responsible for dealing with the
different actions (open, save, sum, etc)
Opening a File
5
setUserValue 5
setCalcValue 7
3 open
4 getLastUserValue
6 getCalcValue
CalcModel
CalcView
getOpenFile 2
CalcController
1 actionPerformed
CalcController: Open a File
import java.awt.event.ActionListener;
public class CalcController implements ActionListener
{
private CalcModel model;
private CalcView view;
public CalcController(CalcModel model, CalcView view)
{
this.model = model;
this.view = view;
}
/**
* Invoked when an event occurs.
*
* @param event
*
The event.
*/
6
Saving a File
7
3 save
CalcModel
CalcView
getSaveFile 2
CalcController
1 actionPerformed
CalcController: Save a File
import java.awt.event.ActionListener;
public class CalcController implements ActionListener
{
private CalcModel model;
private CalcView view;
public CalcController(CalcModel model, CalcView view)
{
this.model = model;
this.view = view;
}
/**
* Invoked when an event occurs.
*
* @param event
*
The event.
*/
8
Sum, Subtract, Multiply, Divide
9
setCalcValue 5
3
sum
4 getCalcValue
CalcModel
CalcView
getUserValue 2
CalcController
1 actionPerformed
CalcController: Other Actions
import java.awt.event.ActionListener;
public class CalcController implements ActionListener
{
private CalcModel model;
private CalcView view;
public CalcController(CalcModel model, CalcView view)
{
this.model = model;
this.view = view;
}
/**
* Invoked when an event occurs.
*
* @param event
*
The event.
*/
10
CalcMVC Source Code
11
actionPerformed
even with only 5 buttons and 2 menu items our
CalcController actionPerformed method is
unwieldy
imagine what would happen if you tried to implement a
Controller this way for a big application
rather than one big actionPerformed method we can
register a different ActionListener for each button
12
Inner Classes
an inner class is a (non-static) class that is defined
inside of another class
public class Outer
{
// Outer's attributes and methods
private class Inner
{ // Inner's attributes and methods
}
}
13
an inner class has access to the attributes and methods
of its enclosing class, even the private ones
public class Outer
{
private int outerInt;
private class Inner
{
public setOuterInt(int num) { outerInt = num; }
}
}
14
note not this.outerInt
CalcController Inner Classes
whenever CalcController receives an event
corresponding to an arithmetic operation it does:
1.
2.
3.
15
asks CalcView for the user value and converts it to a
BigInteger
asks CalcModel to perform the arithmetic operation
updates the calculated value in CalcView
CalcController2: ArithmeticListener
import java.awt.event.ActionListener;
public class CalcController
{
private CalcModel model;
private CalcView view;
public CalcController(CalcModel model, CalcView view)
{
this.model = model;
this.view = view;
this.view.addDivideListener(new DivideListener());
}
// methods...
/**
* Abstract base class for classes that listen
16
Why Use Inner Classes
only CalcController needs to create instances of the
various listeners
making the listeners private inner classes ensures that only
CalcController can instantiate the listeners
the listeners need access to private methods inside of
CalcController (namely getView and getModel)
17
inner classes can access private methods
Recursion
[notes Chapter 8], [AJ 11, 12.2]
18
Printing n of Something
suppose you want to implement a method that prints
out n copies of a string
/**
* Prints the given string n times.
*
* @param s The string to print.
* @param n The number of times to print the string.
* @pre. n >= 0
*/
public static void printIt(String s, int n)
{
for(int i = 0; i < n; i++)
{
System.out.print(s);
}
}
19
A Different Solution
alternatively we can use the following algorithm:
if n == 0 done, otherwise
1.
I.
II.
print the string once
print the string (n – 1) more times
public static void printIt(String s, int n)
{
if(n == 0)
{
return;
}
else
{
System.out.print(s);
printIt(s, n - 1);
}
}
20
Recursion
a method that calls itself is called a recursive method
a recursive method solves a problem by repeatedly
reducing the problem so that a base case can be
reached
printIt("*", 5)
*printIt("*", 4)
**printIt("*", 3)
***printIt("*", 2)
Notice that the number of times
the string is printed decreases
after each recursive call to printIt
****printIt("*", 1)
*****printIt("*", 0) base case Notice that the base case is
*****
eventually reached.
21