Objektorienterade applikationer d2, förel. 6

Slides:



Advertisements
Similar presentations
1 Introduction to Computation and Problem Solving Class 24: Case Study: Building a Simple Postfix Calculator Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
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,
13-Jun-15 Model-View-Controller. 2 Design Patterns The hard problem in O-O programming is deciding what objects to have, and what their responsibilities.
Event Handling Events and Listeners Timers and Animation.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Object-Oriented Analysis and Design
Model View Controller (MVC) Architecture. Terminology and History MVC evolved from Smalltalk-80 Has become a key pattern in web based applications – If.
MVC pattern and implementation in java
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
MVC and MVP. References enter.html enter.html
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Welcome to CIS 083 ! Events CIS 068.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
Chapter 19 Designing the GUI front-end: the Model-View-Controller pattern.
CSE 219 Patterns in Programming More Design Patterns.
GUIs for Video Games. The Plan ● Layout suggestions ● Observer/Observable ● Steps to writing Observer/Observables ● Sample Code ● Practice.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many others 1.
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.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
(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.
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.
The Observer Pattern.
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.
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
Chapter 32 JavaBeans and Bean Events
Behavioral Patterns Algorithms and the assignment of responsibilities among objects Describe not just patterns of objects or classes but also the patterns.
Observer Pattern Context:
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Objektorienterad programmering d2, förel. 8
Chapter 36 JavaBeans and Bean Events
11 Version control (part 2)
Web Design & Development Lecture 11
Slide design: Dr. Mark L. Hornick
A First Look at GUI Applications
A Brief Introduction to Design Patterns
Review: Java GUI Programming
Observer Design Pattern
MVC and Design Patterns
Architectural Patterns for Interactive Software
TK2023 Object-Oriented Software Engineering
Figure 30.2 Layers in NextGen
Model-View-Controller Patterns and Frameworks
Introduction to Behavioral Patterns (1)
Model-View-Controller (MVC) Pattern
Patterns.
Observer Design Pattern
Design pattern Lecture 9.
1 Graphical User Interfaces
Objektorienterad programmering d2, förel. 8
Advanced ProgramMING Practices
OBJECT ARCHITECTURE DESIGN
8. Observer Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
Advanced ProgramMING Practices
Software Design Lecture : 40.
Presentation transcript:

Objektorienterade applikationer d2, förel. 6 6 The MVC model DAT055, 16/17, lp 3

Main concepts to be covered Objektorienterade applikationer d2, förel. 6 Main concepts to be covered Design patterns The Observer design pattern The Model View Controller architecture Designmönster beskriver vanliga Designproblem, och beprövade lösningar Observer minskar kopplingen i en applikation MVC mycket användbart i GUI-sammanhang Bygger på Observer-mönstret Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Using design patterns Inter-class relationships are important, and can be complex. Some relationship recur in different applications. Design patterns help clarify relationships, and promote reuse. Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Pattern structure A pattern name. The problem addressed by it. How it provides a solution: Structures, participants, collaborations. Its consequences. Results, trade-offs. Roller Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Design pattern: Observer Objektorienterade applikationer d2, förel. 6 Design pattern: Observer Supports separation of internal model from a view of that model. Observer defines a one-to-many relationship between objects publisher - subscriber The object-observed notifies all Observers of any state change. Example SimulatorView in the foxes-and-rabbits project. En vy kan vara ett GUI, men även annat Publisher-subscriber är en bra metafor Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Observers Loggning Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Main classes of interest Objektorienterade applikationer d2, förel. 6 Main classes of interest class java.util.Observable Subclasses inherit basic functionality for reporting state changes to observing objects. Independent of the observer’s logic interface java.util.Observer Subclasses implement update funtionality. Many objects can connect to the same observable object. Enkla klasser, men kraftfull idé Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Class relationships Observable Observer * No particular owner-owned relationship Observers do not own the observed objects. Observed objects are unaware of observers. The relation is navigable in both directions Observers know what they observe. Observables must be able to update observers (weak dependency). Tekniskt sett måste observatörerna kunna nås – men på ett uniformt neutralt sätt Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Typical configuration Objektorienterade applikationer d2, förel. 6 Typical configuration o1 : Observer obj : Observable o2 : Observer o3 : Observer ... Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 class Observable public class Observable { - Add observer o to the set of observers for this object public void addObserver(Observer o) - Mark this object as changed public void setChanged() - If this object has changed, then notify all of it’s observers public void notifyObservers() } Detta är Java-specifikt Det finns fler metoder – kolla i API:t! Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 interface Observer public interface Observer { An observable object calls it’s inherited notifyObservers method to have all the object's observers notified of a state change. notifyObservers then calls update for each observer. Parameters: o - the observable object who initiated the call. arg - the argument that was passed to the notifyObservers method by the observable object. notifyObservers forwards this argument to update. void update(Observable o, Object arg); } Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Observer registration Objektorienterade applikationer d2, förel. 6 Observer registration obj : Observable o1 : Observer o2 : Observer o3 : Observer addObserver(o1) addObserver(o2) Sekvensdiagram – dynamiska aspekter Scenariomodellering Här kan det stå this i parameterlistan addObserver(o3) Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 An update scenario obj : Observable o1 : Observer o2 : Observer o3 : Observer someStateChange() setChanged() notifyObservers(x) update(obj,x) Observable har en tabell med observatörer Lätt att implementera! update(obj,x) update(obj,x) Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Typical Observable class Objektorienterade applikationer d2, förel. 6 Typical Observable class public class Obsrvbl extends Observable { private SomeType x; public void someMutator() { ... x = ...; // x has changed, inform observers setChanged(); notifyObservers(x.clone()); ... } Pass some information to the observers. Maybe a copy of x, or something else. Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Typical Observer class Objektorienterade applikationer d2, förel. 6 Typical Observer class public class Obsrvr implements Observer { ... public void update(Observable o,Object arg) { if ( o instanceof Obsrvbl && arg instanceof SomeType) { SomeType x = (SomeType)arg; // take some appropriate action // based on the value of x } else ... } Several objects of different types may be observed by the same observer. Moreover, each observed object may, depending on the situation, pass arguments of different types to update. Hence a case analysis may be necessary. Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Observer registration Objektorienterade applikationer d2, förel. 6 Observer registration Observable obj = new Obsrvbl(); Observer o1 = new Obsrvr(); Observer o2 = new Obsrvr(); Observer o3 = new Obsrvr(); obj.addObserver(o1); obj.addObserver(o2); obj.addObserver(o3); Observer registration Prenumeration Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Alternative observer registration Objektorienterade applikationer d2, förel. 6 Alternative observer registration public class Obsrvr implements Observer { public Obsrvr(Observable x) { ... x.addObserver(this); } public void update(Observable o,Object arg) { Observer registration Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Alternative observer registration Observable obj = new Obsrvbl(); Observer o1 = new Obsrvr(obj); Observer o2 = new Obsrvr(obj); Observer o3 = new Obsrvr(obj); Observer registration Prenumeration Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 The MVC architecture Reenskaug 1979 (Smalltalk-80) Model (content) View (appearance) Controller (user actions) Det finns många varianter på detta! Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Model Model classes take care of data storing and processing business logic domain logic the “database” Applikationsspecifikt Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 View View classes take care of visual aspects Visualization User interface “Model rendering” A model can have many views Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Controller Controller classes take care of the control flow between model and view User actions Event handling Control flow Communication Detta är lite speciellt i Java Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Model (2) Model objects are observable unaware of controller and view part The model is decoupled from the view Här är själva poängen! Koppling vs. Beroende Ideala egenskaper - men kan vara svårt att uppnå i praktiken Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 View (2) View objects are observers of model objects weakly dependent on model and controller Vyn kan vara mer eller mindre domänberoende Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Controller (2) Controller objects update the model with information obtained from the view manage over all control flow, timing, etc Example: control flow in a game Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Variations Variations of the MVC pattern are possible. More or less coupling between model, view and controller: View observes model directly. or: Controller mediates all communication between model and view. Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 MVC architecture View Model 4. update 1. event 3. compute 2. get input Controller Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 MVC class diagram MVC Observable << interface >> Observer + addObserver ( ) : void + setChanged ( ) : void Controller + update ( ) : void + notifyObservers ( ) : void Streckad pil = svagt beroende Dema Koden (följ flödet från knapptryck till uppdatering av vyn) GUI Control Modellklasserna Interfacet först Observer bara för tillståndsförändrande operationer Model View << update >> + computeSomething ( ) : void + update ( ) : void Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Objektorienterade applikationer d2, förel. 6 Consequences Model is completely independent of view. View is more or less dependent of model the view must often have some domain knowledge. Eg. Syntax checking in forms. Controllers are dependent of model (and sometimes of view). Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3 DAT055, 16/17, lp 3

Example: Number series calculator A (very) simple calculator for exploring the prime number and Fibonacci number series. Program design based on the MVC pattern. Explore the mvc1 project! Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3

The calculator GUI :JFrame :class UserInterface next 29 reset 34 :JButton :JTextField :JButton :JPanel :class NumberPane :class NumberPane Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3

Control flow :NextButton- View Controller Model 2 1 next :Number 3 button pushed/ actionPerformed() Model computeNext() 2 1 next reset :Number generator 3 update(digits)/setText(digits) reset() 2 1 :ResetButton- Controller button pushed/ actionPerformed() Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3

<<abstract>> Class design Observable <<abstract>> NumberGenerator + computeNext() + reset() + getValue() PrimeGenerator + computeNext() + reset() + getValue() FibonacciGenerator + computeNext() + reset() + getValue() Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3

<<interface>> Class design (cont.) <<interface>> Observer + update() JFrame JPanel 2 NumberPane + update() 2 UserInterface JButton JTextField PrimeGenerator FibonacciGenerator Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3

Class design (cont.) 2 NumberPane JButton JTextField ActionListener update 2 NumberPane JButton JTextField creates creates ActionListener NextButtonController FibonacciGenerator ResetButtonController Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3

Review The degree of dependency between components is called coupling. Aim for less coupling! The observer design pattern decreases coupling. The MVC architectural pattern decouples the business logic from GUI issues thus easy to modify or replace GUI! Objektorienterade applikationer, DAT055, DAI2,16/17, lp 3