Download presentation
Presentation is loading. Please wait.
1
Model View Controller Pattern Patterns Model-View-Controller
2
A Single Class T result = I; T nextValue = getNextValue() while (!isSentinel(nextValue)) { result = f(result, nextValue); nextValue = getNextValue(..); } Folding Intra-Class Pattern Loan, int new ALoan(0), 1 ALoan.add(), * >= 0
3
Scanning Inter-Class Pattern Scanner Instance Enumeration Interface Scanner User public interface Enumeration { public nextElement(); public boolean hasMoreElements(); }
4
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
5
Book on Design Patterns
6
MVC Pattern Model: APointHistoryModel instance Controller: AConsoleController instance Controller: AnAWTController instance Controller: AnAWTController instance Controller: ASwingController instance View: AConsoleView instance View: ABarChartView instance View: APlotterView instance View: Aplotterview instance
7
Displaying Points
8
Swing button AWT button Swing Detachable Toolbar
9
Displaying Points All four views displayed concurrently and kept in Sync.
10
ObjectEditor-based Structure APointHistory ObjectEditor
11
Reusing Point History APointHistory User Interface Code: Single Main Class
12
More Modular Approach Editor: AConsoleEditor Editor: ASwingPlotterEditor Editor: ABarChartEditor Editor: AnAWTPlotterEditor Composer: Single Main Class APointHistory
13
Sharing the View
14
Sharing the Controller
15
Controller Without View
16
View Without Controller
17
Monolithic Editor AWT Plotter Editor APointHistory
18
Separate View and Controller Controller: AnAWTController View: ABarchartView APointHistory addElement( ) size( ) elementAt( ) Performs InputPerforms Output Write methods Read methods
19
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?
20
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 Listenable Listeners
21
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
22
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
23
MVC Framework Model: APointHistoryModel Instance Controller: AConsoleController instance Controller: AnAWTController instance Controller: AnAWTController instance Controller: ASwingController instance View: AConsoleView instance View: ABarChartView instance View: APlotterView instance View: APlotterView instance Composer: Single Main Class
24
Model Model: APointHistoryModel Instance Controller: AConsoleController instance Controller: AnAWTController instance Controller: AnAWTController instance Controller: ASwingController instance View: AConsoleView instance View: ABarChartView instance View: APlotterView instance View: APlotterView instance Composer: Single Main Class
25
Model Vs PointHistory APointHistory Instance Controller: AConsoleController instance Controller: AnAWTController instance Controller: AnAWTController instance Controller: ASwingController instance View: AConsoleView instance View: ABarChartView instance View: APlotterView instance View: APlotterView instance Composer: Single Main Class
26
Reusing PointHistory PointHistory APointHistory IMPLEMENTS APointHistory Model EXTENDS PointModel EXTENDS IMPLEMENTS
27
Notification Scheme Model: APointHistoryModel Instance View: AConsoleView instance View: ABarChartView instance View: APlotterView instance View: APlotterView instance 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.
28
View with Multiple Models APointHistoryModel Instance PointModel Instance
29
General Notification Scheme Model: APointHistoryModel Instance View: AConsoleView instance View: ABarChartView instance View: APlotterView instance View: APlotterView instance View may have multiple models with common notification method. Notification method parameter indicates which model. Model: PointModel Instance (Animating Point)
30
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 updated Updated object (model) View (console, barchart, plotter..) Common interface of all views
31
APointHistoryModel 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.hasMoreElements()) ((PointHistoryListener) elements.nextElement()).pointHistoryUpdated(this); } Object evaluating expression (Object on which enclosing method executed) Each write method notifies.
32
Console View import 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 ("**********"); }
33
Displaying Points View updated whenever any controller changes model
34
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
35
Console Composer public 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); }
36
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); }
37
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
38
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.