Michael Brockway Advanced Applications Development in Java Model-View-Controller Architecture l MVC Architecture l Delegate-Model Architecture l Observer.

Slides:



Advertisements
Similar presentations
Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
COSC 3461: Module 8 Model-View-Controller (MVC) Architecture.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 36 JTable and JTree.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Graphical User Interface (GUI) Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Metric Converter What layout do we use? What is the class hierarchy? What listeners do we use? The code for this application is on our class web-site.
Object-Oriented Analysis and Design
More OOP Design Patterns
Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.
MVC pattern and implementation in java
C51BR Applications of Spreadsheets 1 Chapter 16 Getting Started Making Charts.
Model View Controller (MVC) Rick Mercer with a wide variety of others 1.
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
MVC CompSci 230 S Software Construction. MVC Architecture  A typical application includes software to  maintain application data,  document text.
MVC and Swing IAT 351 Week 7 Lecture/tutorial Lyn Bartram.
CS 210 Introduction to Design Patterns September 7 th, 2006.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Chapter 19 Designing the GUI front-end: the Model-View-Controller pattern.
Swing, part 2 Tutorial 07 1 / 31 Leonid Barenboim 25/4/2010.
CSE 219 Patterns in Programming More Design Patterns.
COMP 6471 Software Design Methodologies Winter 2006 Dr Greg Butler
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Advanced Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS.
3461 Model-View Controller Advanced GUI concepts.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many others 1.
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
JTree javax.swing.tree javax.swing.event. Components JTree and JTable COMPONENT – Model – Selection Model – Editor – Renderer.
 2002 Prentice Hall. All rights reserved. Chapter 3 – Model-View-Controller Outline 3.1 Introduction 3.2 Model-View-Controller Architecture 3.3 Observable.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
CHAPTER:08 JAVA IDE PROGRAMMING-III Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Object-Oriented Application Development Using VB.NET 1 Chapter 10 VB.NET GUI Components Overview.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 31 JTable and JTree.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
ANDROID AND MODEL / VIEW / CONTROLLER. Slide 2 Design Patters Common solutions to programming problems are called design patterns Design patterns are.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
1 Chapter 24 Advanced Swing Components. 2 Objectives · To understand the Swing model-view-controller architecture (§24.2). · To use JSpinner to scroll.
עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.
Basics of GUI Programming Chapter 11 and Chapter 22.
Model View Controller (MVC) an architecture Rick Mercer with help from many of others 1.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Advanced Swing Trees. Contents I.Introduction to Trees II. Simple Trees III. Editing Trees and Tree Paths IV. Node Enumeration V. Rendering Nodes VI.
CompSci 230 S Software Construction
Observer Pattern Context:
Chapter 3 – Model-View-Controller
Advanced Swing Trees.
Graphical User Interface (pronounced "gooey")
Lecture 27 Creating Custom GUIs
Architectural Patterns for Interactive Software
CompSci 280 S Introduction to Software Development
Design Patterns - A few examples
עקרונות תכנות מונחה עצמים תרגול 8: MVC
Chapter 3 – Model-View-Controller
Metric Converter What layout do we use? What is the class hierarchy?
Advanced ProgramMING Practices
Advanced ProgramMING Practices
Presentation transcript:

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.

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

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

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

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( ) ….

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( ) ….

MVT Architecture7 MVC Example in Java l Refer to DDS section 3.3: figs Account (the model) AccountController (the controller) AccountTextView modifies notifies AccountBarGraphView AssetPiechartView

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

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.

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

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

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.

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

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 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 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.

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.