Upload
gladys-douglas
View
215
Download
0
Embed Size (px)
Citation preview
Michael Brockway
Advanced Applications Development in Java
Model-View-Controller Architecture
MVC ArchitectureDelegate-Model ArchitectureObserver Design PatternMVC and the Observer Design Pattern
Observable class, Observer interfaceMVC Example in JavaExamples in Java of Delegate-Model architecture.
MVT Architecture 2
MVC Architecture A typical application includes software to
maintain application data, document text in a word processor state of a chess game -- positions of pieces
present output outline view or print preview in a word
processor graphical view of chess game
process user input key presses mouse click on controls
Model-View-Controller Architecture is an object-oriented program structure which separates these functions into 3 separate classes The Model contains and maintains the
application data The View provides a visible presentation of the
data (or output) There may be multiple views There may be multiple types of view (View
classes)
Ref: Deitel, Deitel & Santry Chapter 3
MVT Architecture 3
MVC Architecture The Controller is a class which implements
logic for handling user input. There application may have several controllers
On user input, a controller modifies the model it controls, which in turn notifies all its views so each view can update its presentation to reflect the state of the model.
Model
Controller
View
modifies
notifies
MVT Architecture 4
Delegate-Model Architecture
A variation of MVC architecture A single class (application component) has functionality
for both accepting user input and processing it, and presenting output
Java example: javax.swing contains
a JButton class a ButtonModel interface
A Jbutton is delegate for an associated ButtonModel The ButtonModel maintains information on the state
of the button (pressed? Enabled? etc) The JButton presents a graphical view of the button
(pressed/unpressed/enabled/disabled) as well as receiving clicks, doubleclicks.
Model
Delegate
modifies notifies
MVT Architecture 5
Example: Observer Pattern
Defines a 1 to many dependency between objects so that when one changes state, all dependents are notified and updated.
Subject class maintains list list of Observer objects; has methods for adding and removing Observers and notifying them of changes of state in the Concrete Subject.
ConcreteSubject – particular class of interest Observer interface specifies the interface by which
ConcreteObservers are notified ConcreteObserver implements Observer interface and provides
particular behaviours for responding to changes in ConcreteSubject’s state.
<<interface>>
Observer
update(Event)
<<abstract>>Subject
add(Observer)remove(Observer)
notifyAll( )
ConcreteSubject
ConcreteObserver
update(Event)
*
For each observer oo.update( )….
MVT Architecture 6
MVC and the Observer Design Pattern,
The observer design pattern corresponds to the model/view part of MVC.
The subject in the observer design pattern corresponds to the model in an MVC architecture
The observer in the design pattern corresponds to an MVC view.
<<interface>>
java.util. Observer
update(Observable o, Object arg)
<<abstract>>java.util.Observable
add Observer(….)deleteObserver(...)notifyObservers( )
…..
Model
View
update(….)
*
For each observer oo.update( )….
MVT Architecture 7
MVC Example in Java
Refer to DDS section 3.3: figs 3.4 -- 3.10
Account (the
model)
AccountController (the controller)
AccountTextView
modifies
notifies
AccountBarGraphView
AssetPiechartView
MVT Architecture 8
MVC Example in Java
<<interface>>
java.util. Observer
update(Observable o, Object arg)
<<abstract>>java.util.Observable
add Observer(….)deleteObserver(...)notifyObservers( )
…..
Account<<abstract>> AbstractAccountView
update(….)
*
Balance: doublename: string
ConstructorGet & Set methodswithdraw(amount)deposit(amount)
JPanel
account
AccountTextView
AccountBarGraphView
AssetPieChartView
JPanel
AccountController
MVT Architecture 9
MVC Example in Java (ctd)
AccountManager uses these MVC components as follows For test purposes, it makes two Accounts For each account it makes an “account panel”
consisting of an AccountController (JPanel) above an AccountTextView and an
AccountBarGraphView (Jpanels) side by side The two account panels are laid out in the
frame along with an AssetPieChartView giving a composite view of the accounts.
The source files are on this module’s intranet site and on the DD&S CD; printed listings accompany these slides
Account.java, AbstractAccountView.java, AccountTextView.java, AccountBarGraphView.java, AssetPieChartView.java, AccountController.java, AccountManager.java
Try the application. Exercise: modify it so that it manages three
accounts.
MVT Architecture 10
Examples in Java of Delegate-Model architectureJList
A javax.swing.ListModel is an object which maintains a list of objects: it knows how big the list is, which object is where in the list, etc, and can insert/remove objects
A javax.swing.JList is a graphical component giving a view of the list and able to receive user input to modify the list
See DD&S section 3.4 and listing of PhilosophersJList.java
DefaultListModel
Javax.swing.JList (delegate)
modifies notifies
DefaultListModel
<<abstract>>
AbstractListModel
<<interface>>Javax.swing.ListModel
isEmpty(): booleangetSize(): intaddElement(Object)removeElement(Object)elementAt(): ObjectremoveElementAt(int)insertElementAt(int)indexOf(Object): intetc
JList
Javax.swing.JComponent
MVT Architecture 11
Examples in Java of Delegate-Model architectureJTable
A javax.swing.table.TableModel is an object which maintains a table (rows, columns) of objects
A javax.swing.JTable is a graphical component giving a view of the table (and can receive user input to modify it)
See DD&S section 3.5 and listing of PhilosophersJTable.java
DefaultTableModel
Javax.swing.JTable (delegate)
modifies notifies
DefaultTableModel
<<abstract>>
AbstractTableModel
<<interface>>Javax.swing.table.TableModel
addColumn(Object)addRow(Object[])insertRow(int, Object[])removeRow(int)setValueAt(Object, int, int)getValueAt(, int, int): Objectetc
JTable
Javax.swing.JComponent
MVT Architecture 12
Examples in Java of Delegate-Model architectureJTree
A
CB D
E FG
H
I
JK
A
B
E
F
C
G
H
I
D
J
K
A tree is a data structure often represented drawn in one of these ways, with a piece of data at each node.
Examples are
• Family trees
•The structure of an organisation
•The directory structure of a file system in Windows or Unix.
Thinking of “family trees” we get the following terminology:Node C has parent node A and children G, H, I. A is an ancestor of G and G a descendent of A. G, H, I are siblings of each other.The node A (with no parent) is the root. Nodes E, F, G, H, I, J, K (with no children) are leaves.
MVT Architecture 13
Examples in Java of Delegate-Model architectureJTree
DefaultTreeModel
<<interface>>Javax.swing.tree.TreeModel
getPathToRoot(TreeNode) : TreeNode[]removeNodeFromParent( MutableTreeNode node) void insertNodeInto( MutableTreeNode newChild, MutableTreeNode parent, int index)
JTree
Javax.swing.JComponent
getChild(Obj parent, int index): ObjectgetIndexOfChild(Object): intgetRoot(): ObjectisLeaf(Object node): booleangetRoot(TreeNode): Object
getLastSelectedPathComponent : Object
<<interface>>Javax.swing.tree.TreeNode
getChildAt(int index): ObjectgetChildCount(): intgetParent(): TreenodeisLeaf(): booleanetc
<<interface>>MutableTreeNode
Insert(MutableTreeNode chld, int idx)remove(int idx)etc
DefaultMutableTreeNode
Constructor(Object o)add(MutableTreeNode chld)Insert(MutableTreeNode chld, int idx)remove(int idx)getDepth(): intbreadthFirstEnumeration() :EnumerationdepthFirstEnumeration() :Enumerationetc
MVT Architecture 14
Examples in Java of Delegate-Model architectureJTree
A TreeNode is constructed from a given Object Encapsulates the extra functionality for the object to
be stored as a node of a tree DDS section 3.6.1 illustrates a tree with the DefaultTreeModel The object stored at each node are Strings This application allows string object to to added as
nodes and removed from the tree See listing of PhilosophersJTree.java
DDS section 3.6.2 illustrates a custom (non-default) implementation of the TreeModel interface
public class FileSystemModel implements TreeModel ... Displays a disk file system (from some root
directory) as a tree A FileSystemModel object is constructed from a File object as “root directory”.
It implements the TreeModel methods using File class methods of get information about a directory and its children (subdirectories & files)
See listing of FileSystemModel.java. FileFreeFrame.java is a simple application
making use of a FileSystemModel.
MVT Architecture 15
Exercises
Try each of the example programs out and make sure you understand them.
Do DDS exercises 3.6: Make LiabilityPieChartView as a subclass of AssetPieChartView that includes only Accounts with negative balances. Modify class AccountManager to include a LiabilityPieChartView in addition to an AssetPieChartView
3.7: Make a new version of AccountBarGraphView which shows multiple Accounts in a single bar graph [Hint -- imitate AssetPieChartView]
3.8: Enhance 3.7 to allow transfers between accounts. Modify the AccountController to include a JComboBox to select the destination account, and a JButton to perform the transfer.