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.

Slides:



Advertisements
Similar presentations
Software Engineering 2003 Jyrki Nummenmaa 1 OBJECT ARCHITECTURE DESIGN These slides continue with our example application, based on the simplified.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
March Ron McFadyen1 Observer P Also known as Publish-Subscribe Applied in order to implement the Model-View Separation principle (see.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
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.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
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,
14-Jul-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
1 CSE 331 The Strategy and State Patterns slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
MVC pattern and implementation in java
Smith’s Aerospace © P. Bailey & K. Vander Linden, 2005 Architecture: Component and Deployment Diagrams Patrick Bailey Keith Vander Linden Calvin College.
Model View Controller (MVC) Rick Mercer with a wide variety of others 1.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
An Introduction to Software Architecture
COMP 410 & Sky.NET May 2 nd, What is COMP 410? Forming an independent company The customer The planning Learning teamwork.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Events, Listeners, and Callbacks (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
1 CSE 403 Design Patterns and GUI Programming Reading: Object-Oriented Design and Patterns, Ch. 5 (Horstmann) These lecture slides are copyright (C) Marty.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
(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.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
The Model-View Approach in Teaching Java Maria Litvin Phillips Academy, Andover, MA
CSE 219 Patterns in Programming More Design Patterns.
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Oct R McFadyen1 Facade P Problem: There are a set of classes, a subsystem, that you need to interact with for some purpose, but you don’t.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many others 1.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
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.
1 Java Model-View-Controller - see programming.html#concepts.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
1 TCSS 360, Spring 2005 Lecture Notes Design Guidelines 2 Relevant Reading: Object-Oriented Design Heuristics Arthur Riel.
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
6-Jan-16 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
The Observer Pattern.
2/3/ Many facts about MVC A design pattern for the architecture of GUI applications. It works to separate data, UI and its control for a more.
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.
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.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
Slide design: Dr. Mark L. Hornick
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
High degree of user interaction Interactive Systems: Model View Controller Presentation-abstraction-control.
21-Jun-16 Swing Basics and Model-View- Controller.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Observer Pattern Context:
Observer Design Pattern
Architectural Patterns for Interactive Software
What is MVC Category: System MVC=Model-View-Controller
Model-View-Controller (MVC) Pattern
Observer Design Pattern
Model, View, Controller design pattern
Software Design Lecture : 40.
Presentation transcript:

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

2 GUI exercise Let's develop a graphical game of Rock-Paper-Scissors.  Write a GUI for the game using Swing.  Initially make both players choose random moves every time, but then change it to allow the human (first) player to choose his move.  Add the ability to bet on each game and track the player's money.

3 Model and view model: Classes in your system that are related to the internal representation of the state and behavior of the system.  often part of the model is connected to file(s) or database(s)  examples (card game): Card, Deck, Player  examples (bank system): Account, User, UserList view: Classes in that display the state of the model to the user.  generally, this is your GUI (could also be a text UI)  should not contain crucial application data  Different views can represent the same data in different ways Example: Bar chart vs. pie chart  examples: PokerGUI, PacManCanvas, BankApplet

4 Model-view-controller model-view-controller (MVC): Design paradigm for graphical systems that promotes strict separation between model and view. controller: classes that connect model and view  defines how user interface reacts to user input (events)  receives messages from view (where events come from)  sends messages to model (tells what data to display) Model Controller View data for rendering events updates Model View Component Controller

5 Model/view separation Your model classes should NOT:  import graphical packages (java.awt.*, javax.swing.*)  store direct references to GUI classes or components  know about the graphical classes in your system  store images, or names of image files, to be drawn  drive the overall execution of your program Your view/controller classes should:  store references to the model class(es)  call methods on the model to update it when events occur Tricky part: Updating all aspects of the view properly when the state of the model changes...

6 Pattern: Observer objects that listen for updates to the state of others

7 Observer pattern observer: An object that "watches" the state of another object and takes action when the state changes in some way. Problem: You have a model object with a complex state, and the state may change throughout the life of your program.  You want to update various other parts of the program when the object's state changes. Solution: Make the complex model object observable. observable object: An object that allows observers to examine it (notifies its observers when its state changes).  Permits customizable, extensible event-based behavior for data modeling and graphics.

8 Benefits of observer Abstract coupling between subject and observer; each can be extended and reused individually. Dynamic relationship between subject and observer; can be established at run time (can "hot-swap" views, etc) gives more programming flexibility. Broadcast communication: Notification is broadcast automatically to all interested objects that subscribed to it. Can be used to implement model-view separation in Java easily.

9 Observer sequence diagram

10 Observer interface // import java.util.*; public interface Observer { public void update(Observable o, Object arg); } public class Observable {... } Basic idea:  Make your view code implement Observer.  Make your main model class extend Observable.  Attach the view to the model as an observer.  The view's update method will be called when the observable model changes, so write code to handle the change inside update.

11 Observable class Method nameDescription addObserver( Observer ) adds an Observer to this object; its update method is called when notifyObservers is called deleteObserver( Observer ) removes an Observer from this object notifyObservers() notifyObservers( arg ) inform all observers about a change to this object; can pass optional object with more information setChanged() flags that this object's state has changed; must be called prior to each call to notifyObservers

12 Make an Observable model. Write an abstract View superclass which is a JComponent.  make View an observer Extend View for all of your actual views.  Give each its own unique inner components and code to draw the model's state in its own way. Provide a mechanism in GUI to set the view (perhaps via menus).  To set the view, attach it to observe the model. Multiple views

13 Multiple views examples File explorer (icon view, list view, details view) Games (overhead view, rear view, 3D view) Graphs and charts (pie chart, bar chart, line chart)