1 / 15

Model-View-Controller Architecture

Model-View-Controller Architecture. MVC Architecture Delegate-Model Architecture Observer Design Pattern MVC and the Observer Design Pattern Observable class, Observer interface MVC Example in Java Examples in Java of Delegate-Model architecture. MVC Architecture.

vesta
Download Presentation

Model-View-Controller Architecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Model-View-Controller Architecture MVC Architecture Delegate-Model Architecture Observer Design Pattern MVC and the Observer Design Pattern Observable class, Observer interface MVC Example in Java Examples in Java of Delegate-Model 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. Model 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. Controller modifies notifies View MVT Architecture

  4. Model Delegate-Model Architecture Delegate • 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. notifies modifies MVT Architecture

  5. <<abstract>> Subject <<interface>>Observer add(Observer) remove(Observer) notifyAll( ) update(Event) ConcreteObserver update(Event) 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. ConcreteSubject For each observer o o.update( ) …. MVT Architecture

  6. <<interface>> java.util.Observer <<abstract>> java.util.Observable add Observer(….) deleteObserver(...) notifyObservers( ) ….. update(Observable o, Object arg) View update(….) 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. * Model For each observer o o.update( ) …. MVT Architecture

  7. Account (the model) MVC Example in Java • Refer to DDS section 3.3: figs 3.4 -- 3.10 AccountController (the controller) modifies notifies AccountTextView AccountBarGraphView AssetPiechartView MVT Architecture

  8. <<interface>> java.util.Observer <<abstract>> java.util.Observable add Observer(….) deleteObserver(...) notifyObservers( ) ….. update(Observable o, Object arg) MVC Example in Java * JPanel Account <<abstract>>AbstractAccountView Balance: double name: string account Constructor Get & Set methods withdraw(amount) deposit(amount) update(….) AssetPieChartView AccountTextView JPanel AccountBarGraphView 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. DefaultListModel Examples in Java of Delegate-Model architectureJList Javax.swing.JList (delegate) <<interface>> Javax.swing.ListModel • 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 notifies modifies <<abstract>> AbstractListModel DefaultListModel isEmpty(): boolean getSize(): int addElement(Object) removeElement(Object) elementAt(): Object removeElementAt(int) insertElementAt(int) indexOf(Object): int etc Javax.swing.JComponent JList MVT Architecture

  11. DefaultTableModel Examples in Java of Delegate-Model architectureJTable Javax.swing.JTable (delegate) <<interface>> Javax.swing.table.TableModel • 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 notifies modifies <<abstract>> AbstractTableModel DefaultTableModel addColumn(Object) addRow(Object[]) insertRow(int, Object[]) removeRow(int) setValueAt(Object, int, int) getValueAt(, int, int): Object etc Javax.swing.JComponent JTable MVT Architecture

  12. Examples in Java of Delegate-Model architectureJTree A B A E F C D C B G H I I G E F D J H K 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 <<interface>> Javax.swing.tree.TreeModel <<interface>> Javax.swing.tree.TreeNode getChild(Obj parent, int index): Object getIndexOfChild(Object): int getRoot(): Object isLeaf(Object node): boolean getRoot(TreeNode): Object getChildAt(int index): Object getChildCount(): int getParent(): Treenode isLeaf(): boolean etc <<interface>> MutableTreeNode DefaultTreeModel Insert(MutableTreeNode chld, int idx) remove(int idx) etc getPathToRoot(TreeNode) : TreeNode[] removeNodeFromParent( MutableTreeNode node) void insertNodeInto( MutableTreeNode newChild, MutableTreeNode parent, int index) DefaultMutableTreeNode Constructor(Object o) add(MutableTreeNode chld) Insert(MutableTreeNode chld, int idx) remove(int idx) getDepth(): int breadthFirstEnumeration() :Enumeration depthFirstEnumeration() :Enumeration etc Javax.swing.JComponent JTree getLastSelectedPathComponent : Object 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. MVT Architecture

More Related