Model View Controller Pattern - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Model View Controller Pattern

Description:

Model-View-Controller. A Single Class. T result = I; T nextValue = getNextValue ... Controller: ASwingController. instance. View: AConsoleView. instance. View: ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 39
Provided by: Prasun2
Learn more at: http://www.cs.unc.edu
Category:

less

Transcript and Presenter's Notes

Title: Model View Controller Pattern


1
Model View Controller Pattern
  • Patterns
  • Model-View-Controller

2
Folding Intra-Class Pattern
T result I T nextValue
getNextValue() while (!isSentinel(nextValue))
result f(result, nextValue) nextValue
getNextValue(..)
A Single Class
3
Scanning Inter-Class Pattern
Scanner Instance
public interface ltTypegtEnumeration public
ltTypegt nextElement() public boolean
hasMoreElements()
Scanner User
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
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
7
Displaying Points
8
Displaying Points
9
Displaying Points
All four views displayed concurrently and kept in
Sync.
10
ObjectEditor-based Structure
11
Reusing Point History
12
More Modular Approach
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
View ABarchartView
Controller AnAWTController
size( ) elementAt( )
addElement( )
APointHistory
19
Multiple Views and Controllers
Controller AConsoleController
View AConsoleView
Controller AnAWTController
View ABarChartView
APointHistory
Controller AnAWTController
View APlotterView
Controller ASwingController
View APlotterView
Syncing Controllers and Views?
20
Model 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
21
Model 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
22
Class Vs Instance
Controller AConsoleController
View AConsoleView
Controller AnAWTController
View ABarChartView
Model APointHistoryModel
Controller AnAWTController
View APlotterView
Controller ASwingController
View APlotterView
23
MVC 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
24
Model
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
25
Model 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
26
Reusing PointHistory
27
Notification 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
28
View with Multiple Models
29
General 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
30
PointHistoryModel 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)
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.hasMoreEle
ments()) ((PointHistoryListener)
elements.nextElement()).pointHistoryUpdated(this)

32
Console 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
("")
33
Displaying Points
34
Console 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)
35
Console 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)

36
PointHistoryModel 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)
37
Using 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
38
Using 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
Write a Comment
User Comments (0)
About PowerShow.com