Title: Model View Controller Pattern
1Model View Controller Pattern
- Patterns
- Model-View-Controller
2Folding Intra-Class Pattern
T result I T nextValue
getNextValue() while (!isSentinel(nextValue))
result f(result, nextValue) nextValue
getNextValue(..)
A Single Class
3Scanning Inter-Class Pattern
Scanner Instance
public interface ltTypegtEnumeration public
ltTypegt nextElement() public boolean
hasMoreElements()
Scanner User
4Architectural Pattern
- Christopher 79
- Each pattern is a three part rule, which
expresses a relation between a certain context, a
problem, and a solution. - Each pattern describes a problem which occurs
over and over again in our environment, and then
describes the core of a solution to that problem,
in such a way that you can use this solution a
million times over
5Book on Design Patterns
6MVC Pattern
Controller AConsoleController instance
View AConsoleView instance
Controller AnAWTController instance
View ABarChartView instance
Model APointHistoryModel instance
Controller AnAWTController instance
View APlotterView instance
Controller ASwingController instance
View Aplotterview instance
7Displaying Points
8Displaying Points
9Displaying Points
All four views displayed concurrently and kept in
Sync.
10ObjectEditor-based Structure
11Reusing Point History
12More Modular Approach
13Sharing the View
14Sharing the Controller
15Controller Without View
16View Without Controller
17Monolithic Editor
AWT Plotter Editor
APointHistory
18Separate View and Controller
View ABarchartView
Controller AnAWTController
size( ) elementAt( )
addElement( )
APointHistory
19Multiple Views and Controllers
Controller AConsoleController
View AConsoleView
Controller AnAWTController
View ABarChartView
APointHistory
Controller AnAWTController
View APlotterView
Controller ASwingController
View APlotterView
Syncing Controllers and Views?
20Model View Controller Framework
Controller AConsoleController
View AConsoleView
Controller AnAWTController
View ABarChartView
Model APointHistoryModel
Controller AnAWTController
View APlotterView
Changed object notifies views
Controller ASwingController
View APlotterView
21Model View Controller Framework
Controller AConsoleController
View AConsoleView
Controller AnAWTController
View ABarChartView
Model APointHistoryModel
Controller AnAWTController
View APlotterView
Controller ASwingController
View APlotterView
Composer connects models, views, controllers
22Class Vs Instance
Controller AConsoleController
View AConsoleView
Controller AnAWTController
View ABarChartView
Model APointHistoryModel
Controller AnAWTController
View APlotterView
Controller ASwingController
View APlotterView
23MVC Framework
Controller AConsoleController instance
View AConsoleView instance
Controller AnAWTController instance
View ABarChartView instance
Model APointHistoryModel Instance
Controller AnAWTController instance
View APlotterView instance
Controller ASwingController instance
View APlotterView instance
24Model
Controller AConsoleController instance
View AConsoleView instance
Controller AnAWTController instance
View ABarChartView instance
Model APointHistoryModel Instance
Controller AnAWTController instance
View APlotterView instance
Controller ASwingController instance
View APlotterView instance
25Model Vs PointHistory
Controller AConsoleController instance
View AConsoleView instance
Controller AnAWTController instance
View ABarChartView instance
APointHistory Instance
Controller AnAWTController instance
View APlotterView instance
Controller ASwingController instance
View APlotterView instance
26Reusing PointHistory
27Notification Scheme
View AConsoleView instance
- Each view is registered with model.
- Each write method in model calls a notification
method in each view. - Notification method updates display.
View ABarChartView instance
Model APointHistoryModel Instance
View APlotterView instance
- Each student is registered with professors
listserv. - When web page is updated mail sent to students.
- Student reads web page.
View APlotterView instance
28View with Multiple Models
29General Notification Scheme
View AConsoleView instance
- View may have multiple models with common
notification method. - Notification method parameter indicates which
model.
View ABarChartView instance
Model APointHistoryModel Instance
View APlotterView instance
View APlotterView instance
30PointHistoryModel PointHistoryListener
public interface PointHistoryModel extends
PointHistory public void addListener
(PointHistoryListener pointHistoryListener) publ
ic void removeListener (PointHistoryListener
pointHistoryListener)
public interface PointHistoryListener public
void pointHistoryUpdated(PointHistory
pointHistory)
31APointHistoryModel
import java.util.Vector import
java.util.Enumeration public class
APointHistoryModel extends APointHistory
implements PointHistoryModel public void
addElement (int x, int y) super.addElement(x,
y) notifyListeners() Vector listeners
new Vector() public void addListener
(PointHistoryListener pointHistoryListener)
listeners.addElement(pointHistoryListener)
public void removeListener (PointHistoryListener
pointHistoryListener) listeners.removeElement
(pointHistoryListener) void
notifyListeners() Enumeration elements
listeners.elements() while (elements.hasMoreEle
ments()) ((PointHistoryListener)
elements.nextElement()).pointHistoryUpdated(this)
32Console View
import shapes.PointModel public class
APointHistoryConsoleView implements
PointHistoryListener public void
pointHistoryUpdated(PointHistory pointHistory)
System.out.println ("") for (int
i 0 i lt pointHistory.size() i)
PointModel nextPoint (PointModel)
pointHistory.elementAt(i) System.out.println("
(" nextPoint.getX() "," nextPoint.getY()
")") System.out.println
("")
33Displaying Points
34Console Controller
public class APointHistoryConsoleController
implements PointHistoryConsoleController
PointHistory pointHistory public
APointHistoryConsoleController (PointHistory
thePointHistory) pointHistory
thePointHistory public void
processCommands() while (true) System.o
ut.println("Please enter new point") String
input Keyboard.readString() if
(input.equals("quit")) break int x
Integer.parseInt(input) int y
Keyboard.readInt() pointHistory.addElement
(x,y)
35Console Composer
public class APointHistoryConsoleComposer
public static void main (String args)
PointHistoryModel pointHistoryModel new
APointHistoryModel() createConsoleEditor(pointHi
storyModel) public static void
createConsoleEditor(PointHistoryModel
pointHistoryModel) createConsoleView(pointHisto
ryModel) createConsoleController(pointHistoryMod
el).processCommands() public static
PointHistoryListener createConsoleView(PointHistor
yModel pointHistoryModel) PointHistoryListene
r pointHistoryView new APointHistoryConsoleView
() pointHistoryModel.addListener(p
ointHistoryView) return
pointHistoryView public static
PointHistoryConsoleController createConsoleControl
ler(PointHistory pointHistory) return new
APointHistoryConsoleController(pointHistory)
36PointHistoryModel PointHistoryListener
public interface PointHistoryModel extends
PointHistory public void addListener
(PointHistoryListener pointHistoryListener) publ
ic void removeListener (PointHistoryListener
pointHistoryListener)
public interface PointHistoryListener public
void pointHistoryUpdated(PointHistory
pointHistory)
37Using a More Specific Type
public interface PointHistoryModel extends
PointHistory public void addListener
(PointHistoryListener pointHistoryListener) publ
ic void removeListener (PointHistoryListener
pointHistoryListener)
public interface PointHistoryListener public
void pointHistoryUpdated(PointHistoryModel
pointHistoryModel)
pointHistoryUpdated does not call addListener or
removeListener
Allows less polymorphism
38Using Most General Type Possible
public interface PointHistoryModel extends
PointHistory public void addListener
(PointHistoryListener pointHistoryListener) publ
ic void removeListener (PointHistoryListener
pointHistoryListener)
public interface PointHistoryListener public
void pointHistoryUpdated(PointHistory
pointHistory)
pointHistoryUpdated does not call addListener or
removeListener