model view controller pattern patterns model-view-controller
Post on 21-Dec-2015
234 views
TRANSCRIPT
Model View Controller Pattern
• Patterns
• Model-View-Controller
A Single Class
T result = I;T nextValue = getNextValue()while (!isSentinel(nextValue)) { result = f(result, nextValue); nextValue = getNextValue(..);}
Folding Intra-Class PatternLoan, int new ALoan(0), 1
ALoan.add(), *
>= 0
Scanning Inter-Class Pattern
Scanner Instance
Enumeration Interface
Scanner User
public interface <Type>Enumeration { public <Type> nextElement(); public boolean hasMoreElements();
}
Architectural 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
Book on Design Patterns
MVC Pattern
Model:APointHistoryModel
instance
Controller:AConsoleControllerinstance
Controller:AnAWTControllerinstance
Controller:AnAWTControllerinstance
Controller:ASwingControllerinstance
View:AConsoleViewinstance
View:ABarChartViewinstance
View:APlotterViewinstance
View:Aplotterviewinstance
Displaying Points
Displaying Points
Swing button
AWT button
Swing Detachable Toolbar
Displaying Points
All four views displayed concurrently and kept in Sync.
ObjectEditor-based Structure
APointHistory ObjectEditor
Reusing Point History
APointHistoryUser Interface Code:
Single Main Class
More Modular ApproachEditor:AConsoleEditor
Editor:ASwingPlotterEditor
Editor:ABarChartEditor
Editor:AnAWTPlotterEditor
Composer:Single Main
ClassAPointHistory
Sharing the View
Sharing the Controller
Controller Without View
View Without Controller
Monolithic Editor
AWT Plotter Editor
APointHistory
Separate View and Controller
Controller:AnAWTController
View:ABarchartView
APointHistory
addElement( ) size( )elementAt( )
Performs Input Performs Output
Write methods
Read methods
Multiple Views and Controllers
APointHistory
Controller:AConsoleController
Controller:AnAWTController
Controller:AnAWTController
Controller:ASwingController
View:AConsoleView
View:ABarChartView
View:APlotterView
View:APlotterView
Syncing Controllers and Views?
Model View Controller Framework
Model:APointHistoryModel
Controller:AConsoleController
Controller:AnAWTController
Controller:AnAWTController
Controller:ASwingController
View:AConsoleView
View:ABarChartView
View:APlotterView
View:APlotterView
Changed object notifies views
ListenableListeners
Model View Controller Framework
Model:APointHistoryModel
Controller:AConsoleController
Controller:AnAWTController
Controller:AnAWTController
Controller:ASwingController
View:AConsoleView
View:ABarChartView
View:APlotterView
View:APlotterView
Composer:Single Main
Class
Composer connects models, views, controllers
Class Vs Instance
Model:APointHistoryModel
Controller:AConsoleController
Controller:AnAWTController
Controller:AnAWTController
Controller:ASwingController
View:AConsoleView
View:ABarChartView
View:APlotterView
View:APlotterView
Composer:Single Main
Class
Different State
MVC Framework
Model:APointHistoryModel
Instance
Controller:AConsoleControllerinstance
Controller:AnAWTControllerinstance
Controller:AnAWTControllerinstance
Controller:ASwingControllerinstance
View:AConsoleViewinstance
View:ABarChartViewinstance
View:APlotterViewinstance
View:APlotterViewinstance
Composer:Single Main
Class
Model
Model:APointHistoryModel
Instance
Controller:AConsoleControllerinstance
Controller:AnAWTControllerinstance
Controller:AnAWTControllerinstance
Controller:ASwingControllerinstance
View:AConsoleViewinstance
View:ABarChartViewinstance
View:APlotterViewinstance
View:APlotterViewinstance
Composer:Single Main
Class
Model Vs PointHistory
APointHistory Instance
Controller:AConsoleControllerinstance
Controller:AnAWTControllerinstance
Controller:AnAWTControllerinstance
Controller:ASwingControllerinstance
View:AConsoleViewinstance
View:ABarChartViewinstance
View:APlotterViewinstance
View:APlotterViewinstance
Composer:Single Main
Class
Reusing PointHistory
PointHistory APointHistoryIMPLEMENTS
APointHistoryModel
EXTENDS
PointModel
EXTENDS
IMPLEMENTS
Notification Scheme
Model:APointHistoryModel
Instance
View:AConsoleViewinstance
View:ABarChartViewinstance
View:APlotterViewinstance
View:APlotterViewinstance
• Each view is registered with model.
• Each write method in model calls a notification method in each view.
•Notification method updates display.
Model:PointModel
Instance (Animating Point)
• Each student is registered with professor’s listserv.
• When web page is updated mail sent to students.
•Student reads web page.
View with Multiple Models
APointHistoryModel InstancePointModel
Instance
General Notification Scheme
Model:APointHistoryModel
Instance
View:AConsoleViewinstance
View:ABarChartViewinstance
View:APlotterViewinstance
View:APlotterViewinstance
•View may have multiple models with common notification method.
•Notification method parameter indicates which model.
Model:PointModel
Instance (Animating Point)
PointHistoryModel & PointHistoryListener
public interface PointHistoryModel extends PointHistory {public void addListener (PointHistoryListener pointHistoryListener);public void removeListener (PointHistoryListener pointHistoryListener);
}
public interface PointHistoryListener {public void pointHistoryUpdated(PointHistory pointHistory);
}
Called whenever model is updatedUpdated object (model)
View (console, barchart, plotter ..)
Common interface of all views
APointHistoryModelimport 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.hasMoreElements())
((PointHistoryListener) elements.nextElement()).pointHistoryUpdated(this);
}}
Object evaluating expression (Object on which enclosing method executed)
Each write method notifies.
Console Viewimport shapes.PointModel;public class APointHistoryConsoleView implements PointHistoryListener {
public void pointHistoryUpdated(PointHistory pointHistory) {System.out.println ("**********");for (int i = 0; i < pointHistory.size(); i++) {
PointModel nextPoint = (PointModel) pointHistory.elementAt(i);
System.out.println("(" + nextPoint.getX() + "," + nextPoint.getY() + ")");
}System.out.println ("**********");
}}
Displaying Points
View updated whenever any controller changes model
Console Controller
public class APointHistoryConsoleController implements PointHistoryConsoleController {
PointHistory pointHistory ;public APointHistoryConsoleController (PointHistory thePointHistory) {
pointHistory = thePointHistory;}public void processCommands() {
while (true){System.out.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);
}}
}
Output method not called directly
Console Composerpublic class APointHistoryConsoleComposer { public static void main (String args[]) {
PointHistoryModel pointHistoryModel = new APointHistoryModel();createConsoleEditor(pointHistoryModel);
} public static void createConsoleEditor(PointHistoryModel pointHistoryModel) {
createConsoleView(pointHistoryModel);createConsoleController(pointHistoryModel).processCommands();
} public static PointHistoryListener createConsoleView(PointHistoryModel pointHistoryModel) {
PointHistoryListener pointHistoryView = new APointHistoryConsoleView (); pointHistoryModel.addListener(pointHistoryView); return pointHistoryView; } public static PointHistoryConsoleController createConsoleController(PointHistory pointHistory) {
return new APointHistoryConsoleController(pointHistory); }}
PointHistoryModel & PointHistoryListener
public interface PointHistoryModel extends PointHistory {public void addListener (PointHistoryListener pointHistoryListener);public void removeListener (PointHistoryListener pointHistoryListener);
}
public interface PointHistoryListener {public void pointHistoryUpdated(PointHistory pointHistory);
}
Using a More Specific Type
public interface PointHistoryModel extends PointHistory {public void addListener (PointHistoryListener pointHistoryListener);public void removeListener (PointHistoryListener pointHistoryListener);
}
public interface PointHistoryListener {public void pointHistoryUpdated(PointHistoryModel pointHistoryModel);
}
specific type
Allows less polymorphism
pointHistoryUpdated does not call addListener or removeListener
Using Most General Type Possible
public interface PointHistoryModel extends PointHistory {public void addListener (PointHistoryListener pointHistoryListener);public void removeListener (PointHistoryListener pointHistoryListener);
}
public interface PointHistoryListener {public void pointHistoryUpdated(PointHistory pointHistory);
}
More general type
Allows more polymorphism
pointHistoryUpdated does not call addListener or removeListener
PointHistoryModel can be assigned