Model, View, Controller design pattern

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Graphical User Interface (GUI) Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Graphical User Interface (GUI) Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Case Study: Starting the Student Registration System Chapter 3.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
MVC pattern and implementation in java
Model View Controller (MVC) Rick Mercer with a wide variety of others 1.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many others 1.
User Interface Programming in C#: Direct Manipulation Chris North CS 3724: HCI.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Model View Controller MVC Web Software Architecture.
Java Direct Manipulation Chris North cs3724: HCI.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Java Dynamic Graphics.
ANDROID AND MODEL / VIEW / CONTROLLER. Slide 2 Design Patters Common solutions to programming problems are called design patterns Design patterns are.
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.
Model View Controller (MVC) an architecture Rick Mercer with help from many of others 1.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
MiniDraw Introducing a Framework... and a few patterns.
Programming Paradigms, Software Architectural Patterns, and MVC CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
Dive Into® Visual Basic 2010 Express
Introducing a Framework ... and a few patterns
Microsoft Foundation Classes MFC
Design Patterns Source: “Design Patterns”, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides And Created.
LCC 2700: Intro to Computational Media
PYGAME.
A First Look at GUI Applications
Unified Modeling Language
Observer Design Pattern
Lecture 27 Creating Custom GUIs
MVC and other n-tier Architectures
CS101 Introduction to Computing Lecture 19 Programming Languages
Understand Windows Forms Applications and Console-based Applications
CSE 331 Software Design and Implementation
Ch > 28.4.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
What is MVC Category: System MVC=Model-View-Controller
Event Driven Programming
EE 422C Java FX.
CS102 – Bilkent University
Introduction to Computing Using Java
Figure 30.2 Layers in NextGen
CSE 331 Software Design and Implementation
Model-View-Controller Patterns and Frameworks
Model-View-Controller (MVC) Pattern
GoF Design Patterns (Ch. 26)
Professor John Canny Spring 2004 March 5
Events, Event Handlers, and Threads
Advanced ProgramMING Practices
Advanced ProgramMING Practices
Professor John Canny Spring 2003 March 12
MVC overview Model, View, Controller is MVC
Presentation transcript:

Model, View, Controller design pattern CSC 143 Model, View, Controller design pattern

Overview Topics Reading: Displaying dynamic data Model-View Model-View-Controller (MVC) Reading: Textbook: Ch. 20

Review: Repainting the Screen GUI components such as JPanels can draw on themselves using a Graphics context Problem: Drawings aren’t permanent – need to be refreshed Window may get hidden, moved, minimized, etc. Even components like buttons, listboxes, file choosers etc. also must render themselves. Seldom a reason to override paint for such components. There are indirect but more convenient ways to change the rendering. Solution: A "callback" method called paintComponent

Review: Using paintComponent Or just plain paint for older AWT components. Every Component subclass has a paint (paintComponent) method Called automatically by the system when component needs redrawing Program can override paintComponent to get the Graphics and draw what is desired To request the image be updated, send it a "repaint" message paintComponent( ) is eventually called Footnote: "Render" is the word for producing the actual visual image Rendering may take place at multiple levels Ultimate rendering is done by low-level software and/or hardware

Drawing Based on Stored Data Problem: how does paintComponent( ) know what to paint? The picture might need to change over time, too. Answer: we need to store the information somewhere Where? Some possibilities Store detailed graphical information in the component Lines, shapes, colors, positions, etc. Probably in an instance variable, accessible to paintComponent Store underlying information in the component Store objects that know how to paint themselves Store references to the underlying data and query it as needed data object returns information in a form that might differ from the underlying data paintComponent translates the data into graphics All of these approaches can be made to work. What is best?

Model-View-Controller Pattern Idea: want to separate the underlying data from the code that renders it Good design because it separates issues Consistent with object-oriented principles Allows multiple views of the same data Model-View-Controller pattern Originated in the Smalltalk community in 1970’s Used throughout Swing Although not always obvious on the surface Widely used in commercial programming Recommended practice for graphical applications

MVC Overview Model View Controller Contains the “truth” – data or state of the system "Model" is a poor word. "Content" or "underlying data" would be better. View Renders the information in the model to make it visible to users in desired formats Graphical display, dancing bar graphs, printed output, network stream…. Controller Reacts to user input (mouse, keyboard) and other events Coordinates the models and views Might create the model or view Might pass a model reference to a view or vice versa

MVC Interactions and Roles Model Maintains the data in some internal representation Supplies data to view when requested Possibly in a different representation Advanced: Notifies viewers when model has changed and view update might be needed Generally unaware of the display details View Maintains details about the display environment Gets data from the model when it needs to Renders data when requested (by the system or the controller, etc.) Advanced: Catches user interface events and notifies controller Controller Intercepts and interprets user interface events routes information to models and views

MVC vs MV Separating Model from View... ...is just good, basic object-oriented design usually not hard to achieve, with forethought Separating the Controller is a bit less clear-cut May be overkill in a small system. Often the Controller and the View are naturally closely related Both frequently use GUI Components, which the Model is unlikely to do. Model-View Pattern: MV Folds the Controller and the View together.

Implementation Note Model, View, and Controller are design concepts, not class names Might be more than one class involved in each. The View might involve a number of different GUI components Example: JFileChooser MVC might apply at multiple levels in a system A Controller might use a listbox to interact with a user. That listbox is part of the Controller However, the listbox itself has a Model and a View, and possibly a Controller.

Model-View Interaction It's possible to have more than one viewer A viewer tells the model that it wants to be notified when something interesting happens The model contains a list of all interested viewers When something happens (a cycle in the simulation has occurred, for example), the model calls the notify( ) method of each viewer Viewers can react however they like This illustrates the "observer pattern" used heavily in the Java user interface libraries, among other places