based on slides by prof. burton ma - york university · 2015-09-10 · a simple application that...

67
1 Based on slides by Prof. Burton Ma

Upload: others

Post on 11-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

1

Based on slides by Prof. Burton Ma

Page 2: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

2

TV

- on : boolean

- channel : int

- volume : int

+ power(boolean) : void

+ channel(int) : void

+ volume(int) : void

RemoteControl

+ togglePower() : void

+ channelUp() : void

+ volumeUp() : void

Model View

Controller

Page 3: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

http://java.sun.com/developer/technicalArticles/javase/mvc/ 3

Page 4: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Model ◦ Represents state of the application and the rules

that govern access to and updates of state View ◦ Presents the user with a sensory (visual, audio,

haptic) representation of the model state ◦ A user interface element (the user interface for

simple applications) Controller ◦ Processes and responds to events (such as user

actions) from the view and translates them to model method calls

4

Page 5: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

A simple application that lets the user roll a die ◦ When the user clicks the “Roll” button the die is

rolled to a new random value “Event driven programming”

5

Page 6: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

This application is simple enough to write as a single class ◦ SimpleRoll.java

6

Page 7: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

We can also write the application using the model-view-controller pattern ◦ SimpleModel.java ◦ SimpleView.java ◦ SimpleController.java ◦ SimpleApp.java

7

Page 8: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Implement a simple calculator using the model-view-controller (MVC) design pattern

Features: ◦ Sum, subtract, multiply, divide ◦ Clear ◦ Records a log of the user actions Save the log to file Read the log from a file

8

Page 9: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

9

Page 10: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

The calculator application is launched by the user ◦ The text refers to the application as the GUI

The application: 1. Creates the model for the calculator, and then 2. Creates the view of the calculator

10

Page 11: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

public class CalcMVC { public static void main(String[] args) { CalcModel model = new CalcModel(); CalcView view = new CalcView(model); view.setVisible(true); } }

11

Page 12: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Features: ◦ Sum, subtract, multiply, divide ◦ Clear ◦ Records a log of the user actions Save the log to file Read the log from a file

12

Page 13: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

13

CalcModel

- calcValue : BigInteger

- log : ArrayList<String>

+ getCalcValue() : BigInteger

+ getLastUserValue() : BigInteger

+ sum(BigInteger) : void

+ subtract(BigInteger) : void

+ multiply(BigInteger) : void

+ divide(BigInteger) : void

+ clear() : void

+ save(File) : void

+ open(File) : void

- updateLog(String operation, String userValue) : void

BigInteger: Immutable arbitrary-precision integers

Page 14: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

public class CalcModel { private BigInteger calcValue; private ArrayList<String> log; // creates the log and initializes the attributes // using the clear method CalcModel() { this.log = new ArrayList<String>(); this.clear(); }

14

Page 15: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

// sets the calculated value to zero, clears the log, // and adds zero to the log public void clear() { this.calcValue = BigInteger.ZERO; this.log.clear(); this.log.add(this.calcValue.toString()); }

15

Page 16: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

// empty log looks like // [0] // non-empty log looks like: // [0, +, 5, =, 5, -, 3, =, 2, *, 7, =, 14] public BigInteger getLastUserValue() { if(this.log.size() == 1) { return BigInteger.ZERO; } final int last = this.log.size() - 1; return new BigInteger(this.log.get(last - 2)); }

16

Page 17: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

// BigInteger is immutable; no privacy leak public BigInteger getCalcValue() { return this.calcValue; }

17

Page 18: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

// sums the user value with the current calculated value // and updates the log using updateLog public void sum(BigInteger userValue) { this.calcValue = this.calcValue.add(userValue); this.updateLog("+", userValue.toString()); }

18

Page 19: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

public void subtract(BigInteger userValue) { this.calcValue = this.calcValue.subtract(userValue); this.updateLog("-", userValue.toString()); } public void multiply(BigInteger userValue) { this.calcValue = this.calcValue.multiply(userValue); this.updateLog("*", userValue.toString()); }

19

Page 20: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

// cannot divide by zero; options: // 1. precondition userValue != 0 // 2. validate userValue; do nothing // 3. validate userValue; return false // 4. validate userValue; throw exception public void divide(BigInteger userValue) { this.calcValue = this.calcValue.divide(userValue); this.updateLog("/", userValue.toString()); }

20

Page 21: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

// relies on fact ArrayList implements Serializable public void save(File file) { FileOutputStream f = null; ObjectOutputStream out = null; try { f = new FileOutputStream(file); // can throw out = new ObjectOutputStream(f); // can throw out.writeObject(this.log); // can throw out.close(); } catch(IOException ex) {} }

21

Page 22: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

public void open(File file) { FileInputStream f = null; ObjectInputStream in = null; ArrayList<String> log = null; // object to read from file try { f = new FileInputStream(file); // can throw in = new ObjectInputStream(f); // can throw log = (ArrayList<String>) in.readObject(); // can throw in.close(); this.log = log; final int last = this.log.size() - 1; this.calcValue = new BigInteger(this.log.get(last)); } catch(IOException ex) {} catch(ClassNotFoundException ex) {} }

22

Page 23: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

View ◦ Presents the user with a sensory (visual, audio,

haptic) representation of the model state ◦ A user interface element (the user interface for

simple applications)

23

Page 24: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Simple applications often consist of just a single window (containing some controls)

JFrame window with border, title, buttons

24

Page 25: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

A View can be implemented as a subclass of a JFrame

Hundreds of inherited methods but only a dozen or so are commonly called by the implementer (see URL below)

25

View

JFrame

Frame

Window

Container

Component

Object

user interface item

holds other components

plain window

window with title and border

http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html

Page 26: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

The View is responsible for creating: ◦ The Controller ◦ All of the user interface (UI) components menus JMenuBar, JMenu, JMenuItem buttons JButton labels JLabel text fields JTextField file dialog JFileChooser

The View is also responsible for setting up the communication of UI events to the Controller ◦ Each UI component needs to know what object it should

send its events to

26

Page 27: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

public class CalcView extends JFrame { public CalcView(CalcModel model) { super("Simple Calculator"); model.clear(); CalcController controller = new CalcController(model, this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

27

Page 28: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

a menu appears in a menu bar (or a popup menu) each item in the menu is a menu item

28

menu bar JMenuBar

menu JMenu

menu item JMenuItem

JMenuBar

+ add(JMenu)

JMenu

+ add(JMenuItem)

JMenuItem * *

http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html

Page 29: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

To create a menu ◦ Create a JMenuBar ◦ Create one or more JMenu objects Add the JMenu objects to the JMenuBar ◦ Create one or more JMenuItem objectes Add the JMenuItem objects to the JMenu

29

Page 30: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); JMenuItem printMenuItem = new JMenuItem("Print"); fileMenu.add(printMenuItem);

30

Page 31: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

31

import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;

public class CalcView extends JFrame{ private JMenuBar menuBar; private JMenu fileMenu;

public CalcView(CalcModel model) { super("Simple Calculator"); model.clear(); CalcController controller = new CalcController(model, this);

this.menuBar = new JMenuBar(); thi tJM B (thi B )

Page 32: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

A label displays unselectable text and images A text field is a single line of editable text ◦ The ability to edit the text can be turned on and off

32 http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html

label JLabel

label JLabel

text field (edit off) JTextField

text field (edit on) JTextField

http://docs.oracle.com/javase/tutorial/uiswing/components/label.html

Page 33: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

To create a label

JLabel label = new JLabel("text for the label");

To create a text field (20 characters wide)

JTextField textField = new JTextField(20);

33

Page 34: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

34

import java.awt.FlowLayout;

import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JTextField;

public class CalcView extends JFrame{ private JMenuBar menuBar; private JMenu fileMenu; private JTextField calcText; private JTextField userValueText;

public CalcView(CalcModel model) { ("Si l C l l t ")

Page 35: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

A button responds to the user pointing and clicking the mouse on it (or the user pressing the Enter key when the button has the focus)

35 http://docs.oracle.com/javase/tutorial/uiswing/components/button.html

button JButton

Page 36: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

To create a button

JButton button = new JButton("text for the button");

36

Page 37: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

37

import java.awt.FlowLayout;

import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JTextField;

public class CalcView extends JFrame{ private JMenuBar menuBar; private JMenu fileMenu; private JTextField calcText; private JTextField userValueText; private JButton sumButton; private JButton subtractButton; i t JB tt lti l B tt

Page 38: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

A file chooser provides a GUI for selecting a file to open (read) or save (write)

38

file chooser (for choosing a file to open)

JFileChooser

http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

Page 39: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

39

import java.awt.FlowLayout;import java.io.File;

import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JTextField;

public class CalcView extends JFrame{ private JMenuBar menuBar; private JMenu fileMenu; private JTextField calcText; private JTextField userValueText; i t JB tt B tt

Page 40: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

So far we have a View with some UI elements (buttons, text fields, menu items) ◦ Now we need to implement the actions

Each UI element is a source of events ◦ Button pressed, slider moved, text changed (text

field), etc. When the user interacts with a UI element an

event is triggered ◦ This causes an event object to be sent to every

object listening for that particular event The event object carries information about the event

The event listeners respond to the event

40

Page 41: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

41

event source 1

event source 2

event listener A

event listener B

event listener C

event listener D

event object 1

event object 2

Page 42: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

42

"open"

Controller

event object "open" "save"

"sum"

"subtract"

"multiply"

"divide"

"clear" event object "clear"

AbstractButton ActionEvent implements

ActionListener

Page 43: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Each JButton and JMenuItem has two inherited methods from AbstractButton

public void addActionListener(ActionListener l) public void setActionCommand(String actionCommand)

For each JButton and JMenuItem 1. Call addActionListener with the controller as the

argument 2. Call setActionCommand with a string describing what

event has occurred

43

Page 44: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

44

import java.awt.FlowLayout;import java.awt.event.ActionListener;import java.io.File;

import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JTextField;

public class CalcView extends JFrame{ private JMenuBar menuBar; private JMenu fileMenu; private JTextField calcText; i t JT tFi ld V l T t

Page 45: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

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

45

View

JFrame

Controller Model 1 1

View is a subclass of JFrame

Controller has 1 View

Controller has 1 Model

Page 46: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

46

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; }

}

Page 47: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Tecall 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); }

47

Page 48: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

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 ◦ actionPerformed is responsible for dealing with

the different actions (open, save, sum, etc)

48

Page 49: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

49

CalcView

CalcController actionPerformed

getOpenFile

CalcModel open

getLastUserValue

setUserValue

2

1

3

5

4

setCalcValue 7

getCalcValue 6

Page 50: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

50

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. */

Page 51: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

51

CalcView

CalcController actionPerformed

getSaveFile

CalcModel save

2

1

3

Page 52: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

52

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. */

Page 53: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

53

CalcView

CalcController actionPerformed

getUserValue

CalcModel sum

2

1

3

setCalcValue 5

getCalcValue 4

Page 54: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

54

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. */

Page 55: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Source code for the model, view, controller, and app can be found on the course web site with the lectures

55

Page 56: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Even with only 5 buttons and 2 menu items our 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 ◦ Each ActionListener will be an object that has its

own version of the actionPerformed method

56

Page 57: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

57

DivideListener

SubtractListener

SumListener

ArithmeticListener

Page 58: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Whenever a listener receives an event corresponding to an arithmetic operation it does: 1. Asks CalcView for the user value and converts it to

a BigInteger getUserValue method

2. Asks CalcModel to perform the arithmetic

operation doOperation method

3. Updates the calculated value in CalcView

58

Page 59: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

private abstract class ArithmeticListener implements ActionListener { @Override public void actionPerformed(ActionEvent action) { BigInteger userValue = this.getUserValue(); if (userValue != null) { this.doOperation(userValue); this.setCalculatedValue(); } }

59

1.

2. 3.

Page 60: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

/** * Subclasses will override this method to add, subtract, * divide, multiply, etc., the userValue with the current * calculated value. */ protected abstract void doOperation(BigInteger userValue);

60

Page 61: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

private BigInteger getUserValue() { BigInteger userValue = null; try { userValue = new BigInteger(getView().getUserValue()); } catch(NumberFormatException ex) {} return userValue; } private void setCalculatedValue() { getView().setCalcValue(getModel().getCalcValue(). toString()); }

61

Note: these methods need access to the view and model which are associated with the controller.

Page 62: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

How do we give the listeners access to the

view and model? ◦ Could use aggregation ◦ Alternatively, we can make the listeners be inner

classes of the controller

62

Page 63: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

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 } }

63

Page 64: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

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; } } }

64

note not this.outerInt use Outer.this.outerInt

Page 65: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

public class CalcController2 { // ... // inner class of CalcController2 private abstract class ArithmeticListener implements ActionListener { // ... } // inner class of CalcController2 private class SumListener extends ArithmeticListener { @Override protected void doOperation(BigInteger userValue) { // ... } } }

65

Page 66: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

private class SumListener extends ArithmeticListener { @Override protected void doOperation(BigInteger userValue) { if (userValue != null) { getModel().sum(userValue); } } }

66

Page 67: Based on slides by Prof. Burton Ma - York University · 2015-09-10 · A simple application that lets the user roll a die When the user clicks the “Roll” button the die is rolled

Only the controller needs to create instances of the various listeners ◦ I.e., the listeners are not useful outside of the

controller ◦ 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) ◦ Inner classes can access private methods

67