COSC 3461: Module 8 Model-View-Controller (MVC) Architecture.

Slides:



Advertisements
Similar presentations
E-Pay User Guide By Alphabeta Consulting Limited Liability Partnership.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Automating Tasks With Macros
1 Model View Controller. 2 Outline Review Definitions of MVC Why do we need it? Administiriva Changing the display Event flow Dragging at interactive.
Graphical User Interface (GUI) Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
THE SWING UI TOOLKIT Mostly from “The Swing Connection”The Swing Connection.
Graphical User Interface (GUI) Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-Oriented Analysis and Design
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
Model View Controller (MVC) Architecture. Terminology and History MVC evolved from Smalltalk-80 Has become a key pattern in web based applications – If.
Graphical User Interface (GUI) Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
MVC pattern and implementation in java
Systems Analysis – Analyzing Requirements.  Analyzing requirement stage identifies user information needs and new systems requirements  IS dev team.
Model View Controller (MVC) Rick Mercer with a wide variety of others 1.
Software Construction Lecture 10 Frameworks
Welcome to CIS 083 ! Events CIS 068.
Chapter 8: Writing Graphical User Interfaces
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
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.
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
Designing Interface Components. Components Navigation components - the user uses these components to give instructions. Input – Components that are used.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
CSE 219 Patterns in Programming More Design Patterns.
Swing and MVCS CompSci 230 Software Construction.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Automated GUI testing How to test an interactive application automatically?
3461 Model-View Controller Advanced GUI concepts.
GoF: Document Editor Example Rebecca Miller-Webster.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many others 1.
Model View Controller A Pattern that Many People Think They Understand, But Has A Couple Meanings.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 35 MVC and Swing MVC.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Model View Controller MVC Web Software Architecture.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
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.
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.
Office of Housing Choice Voucher Program Voucher Management System – VMS Version Released October 2011.
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.
Key Applications Module Lesson 14 — Working with Tables Computer Literacy BASICS.
Model View Controller (MVC) an architecture Rick Mercer with help from many of others 1.
Oct 091 Example Program DemoInputValidation1.java DemoInputValidation2.java.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Object-Oriented Application Development Using VB.NET 1 Chapter 11 Using Multiple Forms with Problem Domain Classes.
1 Lecture 8: User Interface Components with Swing.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
High degree of user interaction Interactive Systems: Model View Controller Presentation-abstraction-control.
1 Visual Basic: An Object Oriented Approach 7 – The User interface.
Programming Paradigms, Software Architectural Patterns, and MVC CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
CompSci 230 S Software Construction
Support for the Development of Interactive Systems
Java Look-and-Feel Design Guidelines
Lecture 27 Creating Custom GUIs
Advanced Swing Lists.
A Quick Java Swing Tutorial
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Model-View-Controller Patterns and Frameworks
A Quick Java Swing Tutorial
Advanced ProgramMING Practices
Model, View, Controller design pattern
Presentation transcript:

COSC 3461: Module 8 Model-View-Controller (MVC) Architecture

2 The “Traditional” Architecture Applications are designed so that they will appropriately respond to the user’s –requests –queries –commands Input  Processing  Output … These form the set of possible inputs

3 An Alternative The application: –has a model –provides views of the model, each of which has its controller –the actions that the user can perform depend on the view We still have: Input  Processing  Output –Input done with respect to a view –Controller detect user actions and make changes to model –Model notifies views about changes –Views are updated by controllers –The controllers mediate between the views and the model

4 Advantages Independence of the model and views –Can have multiple views simultaneously open –Can create additional views without modifying the model –Can create nested views without modifying the model –Can enhance and/or optimize model without changing the views –Easy to change “look and feel” of views

5 Advantages Enforces Object Oriented (OO) approach Ensures modularity of software components Improves maintainability Promotes code reuse

6 Disadvantages Can be complex Initial design of model is crucial

7 Design Principle The basis for the architecture of an application should be its data (rather than on its user interface) Rationale: Modifications to the interfaces do not warrant changes to the underlying architecture Entirely new interfaces can be developed on top of existing data models The interface: –provides a way for the user to view of the data –affords the means to perform actions on the data (e.g., modify, update), given a particular view …

8 Support for the Design Principle Need a software architecture to support the separation of the application data and the application’s user interface Types of architectures: –Model-View-Controller (MVC) architecture –Modified MVC architecture Quasi-MVC architecture Separable Model (SM) architecture

9 Historical Notes The model-view controller (MVC) architecture was first introduced as the architecture for Smalltalk The Smalltalk environment: –was used to create the first graphical user interfaces –provided support for multiple windows –was developed at the Xerox Palo Alto Research Center (“Xerox PARC”) –didn’t employ the traditional approach to application programs –provided an approach that was subsequently adopted by other designers (e.g., the Apple Macintosh)

10 Historical Notes The modified model-view controller (MVC) architecture was developed by Swing developers See: –Fowler, Amy. ”A Swing Architecture Overview: The Inside Story on JFC Component Design”

11 MVC Schematic Keyboard Mouse Etc. Controller View Model Display

12 Types of Models GUI-data models (or GUI-state models) –the views of these models primarily concern look-and- feel of components and their states Application-data models –quantifiable data that has meaning primarily in the context of an application –can be organized in various ways e.g., lists, tables, trees –the views of these models can vary with respect to which subset of the data is shown and how more than just look-and-feel

13 Examples of Models GUI-data models (or GUI-state models) –the model for a button component ( ButtonModel ) or a slider ( BoundedRangeModel ) –model contains the set of attributes and their current states, also the look and feel Application-data models –in a bank teller application: The set of bank accounts open at a bank; All of the information associated with each of the bank accounts The set of account holders –in a digital circuit simulation application: the set of all chips (e.g., OR, AND, XOR) all the information about their interconnections

14 Tasks for the Model Store and manage data elements –e.g., state information such as the current position of a slider, or whether a button is latched or not Provide methods so that queries about its state can be performed Provide methods so that various attributes of its state can be modified

15 Tasks for the Controller Receive user inputs from mouse and keyboard Consult the view to determine which objects are being manipulated by the user Update the model accordingly Example controller actions: –detects a mouse click; –determines that a button has been pressed and that pressing a button toggles a state; –informs the model that the button state has changed

16 Tasks for the Views Implements a visual display of the model Visual displays can vary –presented as text, in rows and columns –presented as a graphic (e.g., bar chart)

17 The Views A model can have any number of views –the views subscribe to the model Views can differ: –how they present information from the model –what operations the users can perform

18 Example of Views (1) Bank teller application: –The bank teller view –The loan officer view –The bank machine view Digital Circuit Simulation application: –The circuit view –The parts view

19 Example of Views (2) A Check Box: –The Windows view –The Metal view –The Motif view GUI Components: –can think of them as very small, limited applications –they have a model and views –the views differ with respect to “look-and-feel” (superficial differences)

20 Multiple Views Any number of views can subscribe to the model Check Box Model View #1 View #2 View #3

21 Application-data Models in Swing In Swing, models exist as interfaces The interface is implemented in model classes Usually there is a default model class that is automatically associated with a component

22 Types of Application-data models... ListModel –implemented by AbstractListModel –JList uses DefaultListModel, JComboBox uses DefaultComboBoxModel (both extend AbstractListModel ) TableModel –implemented by AbstractTableModel –AbstractTableModel extended by DefaultTableModel –used by JTable TreeModel –implemented by AbstractTreeModel –default model of JTree Document –implemented by AbstractDocument –AbstractDocument extended by PlainDocument –default model of JTextField

23 Views A data model can have any number of views A view subscribes to the model –if the model changes, all subscribing views automatically update themselves Views can differ: –which subset of the data is presented –how the data is presented –what operations are available to the users to perform

24 Example of Views (1) Bank teller application: –The data model is a database of all the bank’s customers and the accounts or services used by each customer –The bank teller view: user can view accounts of any person; can modify the balance of any account. –The loan officer view: user can view all accounts, can retrieve credit history, can open a new account (loan, line of credit), can transfer amounts between accounts. –The bank machine view: user can view account information for a particular person; can deposit, withdraw, transfer for that person’s account

25 Example of Views (2) Digital Circuit Simulation: –The data model is a list of all of the chips in the circuit and the connections (wires) between them –The circuit view: the user sees a graphical layout of the entire circuit; can add, change, or remove chips; can change the wire connections –The parts view: the user sees a list of all the different types of chips (and a count for each one) and an estimate of the amount of wire the circuit, if implemented, would use; can print the list out

26 Multiple Simultaneous Views Each view must ensure that its appearance reflects the state of the model. The controllers mediate between the views and the model

27 Table Models and Their Views An instance of a TableModel has: –cells, arranged in columns and rows –the values of the cells are instances of Object An instance of a JTable has: –an underlying data model, an instance of a TableModel –an instance of a TableModelListener, which is installed on the instance’s TableModel NOTE! There is nothing to stop you from (1) creating another instance of a JTable, and (2) assigning it’s data model to be the same instance of TableModel above

28 Custom Table Models Suppose we create an instance of a customized TableModel –instances of JTable that uses that instance as its data model This is done in example DemoTableModel.java Note: –The JTable ’s data model is an instance of OurCustomTableModel –when we modify a cell of the JTable, the setValueAt method of OurCustomTableModel is invoked THIS CAUSES THE DATA MODEL TO BE UPDATED –Also, setValueAt causes a TableModelEvent to be generated –The TableModelListener that is associated with the JTable (and is installed on OurCustomTableModel ) receives this event, the tableChanged method is invoked THIS CAUSES THE VIEW TO BE UPDATED

29 Example DemoTableModel.java

30 Subscribe-Notify Suppose the model changes… How does the system ensure that all the other views reflect the updated model? –The application follows the subscribe-notify protocol. –The model notifies each subscribed view that it has changed. Two types of notification: Lightweight, Stateful –Each view then has the opportunity to update itself.

31 CONTROLLERVIEW MODEL 1) Update2) Notify 4) Data 3) Request Lightweight Notification Controller detects user action Controller tells the Model to change (1) Model says “I changed” –The notification doesn’t say how Model notifies each View of this (2) Views that need to update ask the Model for data (3) The Model sends the data to the requesting Views (4)

32 Stateful Notification Controller detects user action Controller tells the Model to change (1) Model says “I changed, and I changed in this way” –The notification includes all necessary information for the View to update itself. Model notifies each View of this (2) CONTROLLERVIEW MODEL 1) Update2) Notify

33 Lightweight notification –A single event instance can be used for all notifications –Desirable when notifications are frequent Stateful notification –A different event instance for each notification –Desirable for complex models Lightweight vs. Stateful Notification

34 MVC and Swing Swing designers found it difficult to write a generic controller that didn’t know the specifics about the view They collapsed the view and controller into a single user interface object –UI delegate –UI Object –UI delegate object –Delegate object The UI is delegated to this object

35 MVC and Swing (2) Keyboard Mouse Etc. Controller View Model Display Swing component UI delegate

36 How to Ignore Models Many applications need not worry about models Most component classes provide the model API directly Example from code that implements JSlider class: public int getValue() { return getModel().getValue(); } Applications that use JSlider can simply: JSlider slider = new JSlider(); int value = slider.getValue();

37 Recall Data Model Validation DemoInputValidation3.java JTextField ’s default data model is PlainDocument. We can create a custom data model for a JTextField by creating our own data model and substituting it for PlainDocument

38 Multiple Views DemoTwoViews.java

39 Example: Radio Button The attributes of a Radio Button are defined in ButtonModel –It is an interface defined in Swing –It has a default implementation: DefaultButtonModel

40 Example: Radio Button Did you notice? –the ButtonModel (the model of a Radio Button) doesn’t say anything about how the radio button should appear! The Swing code for JRadioButton and JRadioButtonMenuItem take care of the appearance of DefaultButtonModel –Swing components, in general, take care of the appearance of their corresponding, underlying data models

41 Example: Radio Button The instances of JRadioButtonMenuItem are “interested parties” –if the ButtonModel changes, they want to know about it e.g., if the state changes to selected, then appearance of models needs to be updated The Swing components take responsibility for hooking up the appropriate listeners and for repainting themselves when the model changes The ButtonModel doesn’t know anything about the entities that are interested in it It only knows that there are interested parties Keeps a list of listeners that are interested in knowing when its state has changed