Controller: takes input from the user and decides what to do
View: knows how to display the interface to the user
Model: the domain information that the program manipulates
Glue of the system
Controls how and when the model is used
Controls what must be shown to the view and when (in some cases)
Controls what action must be taken when user interacts with system (in some cases)
Controls the sequence of operations in an application(in some cases)
Model: Offers actual operations
View: Presents results
Controller: Delegates to model and view
PyramidSolitaireController
“Controls” the game
Uses the model and the view in specific sequence
The sequence of the game is determined by the controller, not the user
Determines the sequence of operations of the system
Determines when and how the system interacts with something external
Suitable for pre-baked “rules-based” applications, like games
Challenges:
How to truly separate controller from view so each one is replaceable?
What to do when system is reactive in nature (to user input)?
User interaction decides program behavior, not controller
Examples: Microsoft Word, IntelliJ
Controller gets control when external input is given
Controller methods are called as a response to external input
Such methods in the controller are called callbacks
GUI programs typically work this way
User clicks a button, moves the mouse ⇒ a controller method is called
In OO GUIs, callbacks are often wrapped in classes called listeners
Look at MVCExample code
Who creates the controller, model, view objects and sets the program in motion?
For standalone Java program: the main
method
public class MainRunner {
public static void main(String[] args) {
IModel theModel = makeAModel();
IView aView = makeAView();
IController theController = makeController(theModel, aView);
theController.go();
}
}
Printing to console System.out.print
Reading from keyboard Scanner
How to we test this (using a tester, not a human)?
A component that can “replace” an actual component
For testing purposes
As a placeholder for simultaneous development (also called a stub)
Design that facilitates replacement promotes mocking
Good encapsulation ⇒ fixed role ⇒ easier to replace
Design by interface ⇒ Not dependent on specific implementation
Refer to components using interfaces ⇒ work with any implementation of that interface
Mock implements actual interface, but does so simply or to enable testing
Use mocks to
Test components that otherwise require I/O
Emulate a component that is being concurrently designed/implemented