Download presentation
Presentation is loading. Please wait.
Published byMelissa Harrington Modified over 6 years ago
1
Architectural Patterns for Interactive Software
Yonglei Tao School of Computing and Info Systems
2
Interactive Software Design
Application data and logic Relatively stable Look and feel Support multiple views Evolve in response to changes in requirements, user profiles, display and interaction mechanisms, and … Desirable properties for software design Domain objects – reusable User interfaces – extensible, pluggable
3
Model-View-Controller
A common structure for interactive applications Originally intended for applications with multiple views for the same data Benefits De-couple domain objects from user interfaces Reduce complexity Support reuse Allow separate development
4
Model, View, and Controller
Encapsulate fundamental abstractions for interactive applications the model maintains application data the view is responsible for its visual presentation the controller handles events for views
5
An Example Model – class Counter View Controller int Value
setValue(), getValue(), inc(), and dec() View Shows the current value of a Counter Controller Handles user requests Controller is listener in Java.
6
Design Issues - Dependencies
1..* View Screen update() display 1 getValue() Model 1 1..* inc() Controller Mouse click a button
7
The Observer Pattern Problem How to handle such a relationship?
A subject is observed by several observers When its state changes, all observers are notified and updated to reflect the change automatically How to handle such a relationship? One to many Two-way dependency
9
A MVC Example
10
// CounterModel.java - an Observable class for a counter
import java.util.Observable; public class CounterModel extends Observable { private int value; public CounterModel () { setValue( 0 ); } public int getValue () { return value; public void setValue (int newValue) { value = newValue; setChanged(); notifyObservers(); public void inc () { setValue( getValue() + 1 ); public void dec () { setValue ( getValue() - 1 ); public void reset () { setValue ( 0 );
11
/** CounterView.java - an Observer class for a counter */
import java.util.*; import java.lang.*; public class CounterView extends JFrame implements Observer { private CounterModel theModel; public CounterView(CounterModel model) throws NullPointerException { initComponents(); theModel = model; theModel.addObserver(this); } private void initComponents() { … } // automatically generated private void incButtonActionPerformed(ActionEvent evt) { theModel.inc(); private void decButtonActionPerformed(ActionEvent evt) { theModel.dec();
12
private void resetButtonActionPerformed(ActionEvent evt) {
theModel.reset(); } private void exitButtonActionPerformed(ActionEvent evt) { System.exit(0); /** update the display with the current value of the counter */ public void update (Observable observable, Object obj) { valueLabel.setText(theModel.getValue() + ""); public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { CounterModel model = new CounterModel(); public void run() { new CounterView(model).setVisible(true); }); private JButton decButton, exitButton, incButton, resetButton; private JLabel labelForValue, titleLabel, valueLabel;
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.