Download presentation
Presentation is loading. Please wait.
Published byGladys Douglas Modified over 9 years ago
1
Michael Brockway Advanced Applications Development in Java Model-View-Controller Architecture l MVC Architecture l Delegate-Model Architecture l Observer Design Pattern l MVC and the Observer Design Pattern l Observable class, Observer interface l MVC Example in Java l Examples in Java of Delegate-Model architecture.
2
MVT Architecture2 MVC Architecture l A typical application includes software to n maintain application data, u document text in a word processor u state of a chess game -- positions of pieces n present output u outline view or print preview in a word processor u graphical view of chess game n process user input u key presses u mouse click on controls l Model-View-Controller Architecture is an object-oriented program structure which separates these functions into 3 separate classes n The Model contains and maintains the application data n The View provides a visible presentation of the data (or output) u There may be multiple views u There may be multiple types of view (View classes) l Ref: Deitel, Deitel & Santry Chapter 3
3
MVT Architecture3 MVC Architecture n The Controller is a class which implements logic for handling user input. u There application may have several controllers n 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
4
MVT Architecture4 Delegate-Model Architecture l A variation of MVC architecture l A single class (application component) has functionality for both accepting user input and processing it, and presenting output l Java example: n javax.swing contains u a JButton class u a ButtonModel interface n A Jbutton is delegate for an associated ButtonModel n The ButtonModel maintains information on the state of the button (pressed? Enabled? etc) n The JButton presents a graphical view of the button (pressed/unpressed/enabled/disabled) as well as receiving clicks, doubleclicks. Model Delegate modifies notifies
5
MVT Architecture5 Example: Observer Pattern l Defines a 1 to many dependency between objects so that when one changes state, all dependents are notified and updated. l 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. l ConcreteSubject – particular class of interest l Observer interface specifies the interface by which ConcreteObservers are notified l ConcreteObserver implements Observer interface and provides particular behaviours for responding to changes in ConcreteSubject’s state. > Observer update(Event) > Subject add(Observer) remove(Observer) notifyAll( ) ConcreteSubject ConcreteObserver update(Event) * For each observer o o.update( ) ….
6
MVT Architecture6 MVC and the Observer Design Pattern, l The observer design pattern corresponds to the model/view part of MVC. l The subject in the observer design pattern corresponds to the model in an MVC architecture l The observer in the design pattern corresponds to an MVC view. > java.util. Observer update(Observable o, Object arg) > java.util.Observable add Observer(….) deleteObserver(...) notifyObservers( ) ….. Model View update(….) * For each observer o o.update( ) ….
7
MVT Architecture7 MVC Example in Java l Refer to DDS section 3.3: figs 3.4 -- 3.10 Account (the model) AccountController (the controller) AccountTextView modifies notifies AccountBarGraphView AssetPiechartView
8
MVT Architecture8 MVC Example in Java > java.util. Observer update(Observable o, Object arg) > java.util.Observable add Observer(….) deleteObserver(...) notifyObservers( ) ….. Account > AbstractAccountView update(….) * Balance: double name: string Constructor Get & Set methods withdraw(amount) deposit(amount) JPanel account AccountTextView AccountBarGraphView AssetPieChartView JPanel AccountController
9
MVT Architecture9 MVC Example in Java (ctd) l AccountManager uses these MVC components as follows n For test purposes, it makes two Accounts n For each account it makes an “account panel” consisting of u an AccountController (JPanel) above u an AccountTextView and an AccountBarGraphView (Jpanels) side by side n The two account panels are laid out in the frame along with an AssetPieChartView giving a composite view of the accounts. l The source files are on this module’s intranet site and on the DD&S CD; printed listings accompany these slides n Account.java, AbstractAccountView.java, AccountTextView.java, AccountBarGraphView.java, AssetPieChartView.java, AccountController.java, AccountManager.java l Try the application. l Exercise: modify it so that it manages three accounts.
10
MVT Architecture10 Examples in Java of Delegate-Model architecture JList l 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 l A javax.swing.JList is a graphical component giving a view of the list and able to receive user input to modify the list l See DD&S section 3.4 and listing of PhilosophersJList.java DefaultListModel Javax.swing.JList (delegate) modifies notifies DefaultListModel > AbstractListModel > Javax.swing.ListModel isEmpty(): boolean getSize(): int addElement(Object) removeElement(Object) elementAt(): Object removeElementAt(int) insertElementAt(int) indexOf(Object): int etc JList Javax.swing.JComponent
11
MVT Architecture11 Examples in Java of Delegate-Model architecture JTable l A javax.swing.table.TableModel is an object which maintains a table (rows, columns) of objects l A javax.swing.JTable is a graphical component giving a view of the table (and can receive user input to modify it) l See DD&S section 3.5 and listing of PhilosophersJTable.java DefaultTableModel Javax.swing.JTable (delegate) modifies notifies DefaultTableModel > AbstractTableModel > Javax.swing.table.TableModel addColumn(Object) addRow(Object[]) insertRow(int, Object[]) removeRow(int) setValueAt(Object, int, int) getValueAt(, int, int): Object etc JTable Javax.swing.JComponent
12
MVT Architecture12 Examples in Java of Delegate-Model architecture JTree A C B D E F G H I J K 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.
13
MVT Architecture13 Examples in Java of Delegate-Model architecture JTree DefaultTreeModel > Javax.swing.tree.TreeModel getPathToRoot(TreeNode) : TreeNode[] removeNodeFromParentremoveNodeFromParent( MutableTreeNode node) void insertNodeInto(MutableTreeNode MutableTreeNode newChild,MutableTreeNode MutableTreeNode parent, intMutableTreeNode index) JTree Javax.swing.JComponent getChild(Obj parent, int index): Object getIndexOfChild(Object): int getRoot(): Object isLeaf(Object node): boolean getRoot(TreeNode): Object getLastSelectedPathComponent : Object > Javax.swing.tree.TreeNode getChildAt(int index): Object getChildCount(): int getParent(): Treenode isLeaf(): boolean etc > MutableTreeNode Insert(MutableTreeNode chld, int idx) removeremove(int idx) etc DefaultMutableTreeNode Constructor(Object o) add(MutableTreeNode chld) Insert(MutableTreeNode chld, int idx) removeremove(int idx) getDepth(): int breadthFirstEnumeration() :Enumeration depthFirstEnumeration() :Enumeration etc
14
MVT Architecture14 Examples in Java of Delegate-Model architecture JTree l A TreeNode is constructed from a given Object n Encapsulates the extra functionality for the object to be stored as a node of a tree l DDS section 3.6.1 illustrates a tree with the DefaultTreeModel n The object stored at each node are Strings n This application allows string object to to added as nodes and removed from the tree See listing of PhilosophersJTree.java l DDS section 3.6.2 illustrates a custom (non-default) implementation of the TreeModel interface public class FileSystemModel implements TreeModel... n Displays a disk file system (from some root directory) as a tree n A FileSystemModel object is constructed from a File object as “root directory”. n 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.
15
MVT Architecture15 Exercises l Try each of the example programs out and make sure you understand them. l Do DDS exercises n 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 n 3.7: Make a new version of AccountBarGraphView which shows multiple Accounts in a single bar graph [Hint -- imitate AssetPieChartView] n 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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.