1 Introduction Introduction to MVC to MVC Observer Pattern Observer Pattern After this lecture you should understand the following: the Model-View-Controller pattern: components and their roles the observer pattern and its use in the MVC pattern Also, you should be able to: write a graphical user interface for a simple application using the MVC architecture Reference: The Model-View-Controller (MVC) is a commonly used and powerful architecture for GUIs. How does it work? http://ootips.org/mvc-pattern.html
26
Embed
Introduction to MVC Observer Pattern - Informationstechnikasbeck/gs/2 MVC.pdf · 1 Introduction to MVC Observer Pattern After this lecture you should understand the following: the
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Introduction Introduction to MVCto MVCObserver Pattern Observer Pattern
� After this lecture you should understand the following:
� the Model-View-Controller pattern: components and their roles
� the observer pattern and its use in the MVC pattern
� Also, you should be able to:
� write a graphical user interface for a simple application using the MVC architecture
� Reference: The Model-View-Controller (MVC) is a commonly used and powerful architecture for GUIs. How does it work?http://ootips.org/mvc-pattern.html
2
What areWhat are patternspatterns??
Architect Christopher Alexander (1977):
A pattern is a three-part rule,which expresses a relation between
� a certain context,
� a problem, and
� a solution.
3
Patterns in SW Patterns in SW developmentdevelopment
� Pattern „bible“: Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Design Patterns Elements of Reusable Object–Oriented Software.Addison-Wesley, 1995
� Patterns are ways to describe best practices, good designs, and capture experience in a way that it is possible for others to reuse this experience.
4
Pattern: ObserverPattern: Observer
� Establishes a 1:m relation
� Publishes changes of an object to other objects automatically
� Keep objects separate from each other
Question:What is the advantage of independent objects?
Answer:
enables certain objects to be pulled out and replaced without programming
changes
5
ExampleExample ObserverObserver
Example from Gamma et al.
a graph and a table could both display and edit the same data
When the model is updated, all Views are informed and given a chance to update themselves.
8
MVC MVC –– Model View ControllerModel View Controller
� Design Pattern for dialog-oriented and distributed
applications (i.e. web development)
� The Model View Controller is a technique used to separate
� Business logic (the Model) from
� User Interface (the View) and
� program progression/flow (the Control).
� the Observer Pattern provides the basis
9
Model View ControllerModel View Controller
Users interact with the controller.
It interprets mouse movement, clicks, keystrokes, etc
Communicates those activities to the model – eg: delete row, insert row, etc
It’s interaction with the model indirectly causes the View(s) to update
10
Model View ControllerModel View Controller
Model
View
11
merges View and Controller together into one object – a delegate
UIFactory provides appropriate delegates according to Look&Feel
MVC in Java MVC in Java –– Java Java DelegateDelegate ModelModel
MVC in Java
•Swing often merges the View and Controller together into one object.
•Sun call’s this a delegate.
•The delegate interprets the user’s interactions and updates the model
•It also handles the display
•Note that this still allows multiple views on the same model.
•It has nothing to do with C# delegates.
12
MVC MVC ExampleExample : Table : Table modelmodel� Saves data in a table
� Add and update data
� administer attributes, i.e. „is edible“
� Example / Default model: DefaultTableModel
class MyTableModel extends DefaultTableModel {public final String[] heads = {"Nr", "Rand"};
public MyTableModel(int rows) {super.setColumnIdentifiers(heads);int i;for (i = 0; i < rows; i++) {
Object a[] = new Object[heads.length];a[0] = new Integer(i + 1);a[1] = new Double(Math.random());super.addRow(a);
} } }
13
MVC MVC ExampleExample : : Assign modelAssign model
public MyFrame(MyTableModel myModel) {try {this.jTable.setModel(myModel);myModel.addTableModelListener(this.sumTextField);myModel.fireTableDataChanged();jbInit();…