Introducing OOP - Computing at Northumbria

Download Report

Transcript Introducing OOP - Computing at Northumbria

Advanced Applications Development in Java

Model-View-Controller Architecture

 MVC Architecture  Delegate-Model Architecture  Observer Design Pattern  MVC and the Observer Design Pattern  Observable class,

Observer

interface  MVC Example in Java  Examples in Java of Delegate-Model architecture.

Michael Brockway

MVC Architecture

  

A typical application includes software to

   maintain application data,  document text in a word processor  state of a chess game -- positions of pieces present output  outline view or print preview in a word processor  graphical view of chess game process user input  key presses  mouse click on controls

Model-View-Controller Architecture is an object-oriented program structure which separates these functions into 3 separate classes

  The Model contains and maintains the application data The View provides a visible presentation of the data (or output)  There may be multiple views  There may be multiple types of view (View classes)

Ref: Deitel, Deitel & Santry Chapter 3

MVT Architecture 2

MVC Architecture

 The Controller is a class which implements logic for handling user input.

 There application may have several controllers  On user input, a controller modifies the model it controls, which in turn notifies all its views so each view can update its presentation to reflect the state of the model.

Controller modifies

MVT Architecture

Model notifies View

3

Delegate-Model Architecture

modifies Delegate notifies Model

   A variation of MVC architecture A single class (application component) has functionality for both accepting user input and processing it, and presenting output Java example:   javax.swing contains  a JButton class  a

ButtonModel

interface A Jbutton is delegate for an associated ButtonModel   The ButtonModel maintains information on the state of the button (pressed? Enabled? etc) The JButton presents a graphical view of the button (pressed/unpressed/enabled/disabled) as well as receiving clicks, doubleclicks.

MVT Architecture 4

Example: Observer Pattern

<> Observer update(Event)

*

<> Subject

add(Observer) remove(Observer) notifyAll( ) ConcreteObserver update(Event) For each observer o o.update( ) ….

ConcreteSubject      Defines a 1 to many dependency between objects so that when one changes state, all dependents are notified and updated.

Subject class maintains list list of

Observer

for adding and removing Observers and notifying them of changes of state in the Concrete Subject. objects; has methods ConcreteSubject – particular class of interest

Observer

interface specifies the interface by which ConcreteObservers are notified ConcreteObserver implements particular behaviours for responding to changes in ConcreteSubject’s state.

Observer

interface and provides MVT Architecture 5

MVC and the Observer Design Pattern,

   The observer design pattern corresponds to the model/view part of MVC.

The

subject

in the observer design pattern corresponds to the

model

in an MVC architecture The

observer

in the design pattern corresponds to an MVC

view

.

<> java.util.

Observer update(Observable o, Object arg)

*

<> java.util.Observable

add Observer(….) deleteObserver(...) notifyObservers( ) …..

View update(….) For each observer o o.update( ) ….

Model MVT Architecture 6

MVC Example in Java

 Refer to DDS section 3.3: figs 3.4 -- 3.10

AccountController (the controller) modifies Account (the model) notifies AccountTextView AccountBarGraphView AssetPiechartView

MVT Architecture 7

MVC Example in Java

<> java.util.

Observer update(Observable o, Object arg)

* JPanel

<> java.util.Observable

add Observer(….) deleteObserver(...) notifyObservers( ) …..

<>

AbstractAccountView account update(….) Account Balance: double name: string Constructor Get & Set methods withdraw(amount) deposit(amount) AssetPieChartView AccountTextView AccountBarGraphView JPanel AccountController MVT Architecture 8

MVC Example in Java (ctd)

   

AccountManager uses these MVC components as follows

  For test purposes, it makes two Accounts For each account it makes an “account panel” consisting of  an AccountController (JPanel) above  an AccountTextView and an AccountBarGraphView (Jpanels) side by side  The two account panels are laid out in the frame along with an AssetPieChartView giving a composite view of the accounts.

The source files are on this module’s intranet site and on the DD&S CD; printed listings accompany these slides  Account.java, AbstractAccountView.java, AccountTextView.java, AccountBarGraphView.java, AssetPieChartView.java, AccountController.java, AccountManager.java

Try the application.

Exercise: modify it so that it manages three accounts.

MVT Architecture 9

Examples in Java of Delegate-Model architecture

JList

Javax.swing.JList (delegate)

<>

Javax.swing.ListModel

modifies notifies

<> AbstractListModel

DefaultListModel

Javax.swing.JComponent

JList DefaultListModel isEmpty(): boolean getSize(): int addElement(Object) removeElement(Object) elementAt(): Object removeElementAt(int) insertElementAt(int) indexOf(Object): int etc    A javax.swing.ListModel is an object which maintains a list of objects: it knows how big the list is, which object is where in the list, etc, and can insert/remove objects A javax.swing.JList is a graphical component giving a view of the list and able to receive user input to modify the list See DD&S section 3.4 and listing of PhilosophersJList.java

MVT Architecture 10

Examples in Java of Delegate-Model architecture

JTable

Javax.swing.JTable (delegate)

<>

Javax.swing.table.TableModel

modifies notifies

<> AbstractTableModel

DefaultTableModel

Javax.swing.JComponent

DefaultTableModel addColumn(Object) addRow(Object[]) insertRow(int, Object[]) removeRow(int) setValueAt(Object, int, int) getValueAt(, int, int): Object etc JTable    A javax.swing.table.TableModel is an object which maintains a table (rows, columns) of objects A javax.swing.JTable is a graphical component giving a view of the table (and can receive user input to modify it) See DD&S section 3.5 and listing of PhilosophersJTable.java

MVT Architecture 11

Examples in Java of Delegate-Model architecture

JTree

A B A E F C B C D G G H I E F I H J D K J K

A

tree

is a data structure often represented drawn in one of these ways, with a piece of data at each node. • Examples are • Family trees • The structure of an organisation The directory structure of a file system in Windows or Unix.

Thinking of “family trees” we get the following terminology:Node C has

parent

node A and

children

G, H, I. A is an

ancestor

of G and G a

descendent

of A. G, H, I are

siblings

of each other.

The node A (with no parent) is the

root

. Nodes E, F, G, H, I, J, K (with no children) are

leaves

.

MVT Architecture 12

Examples in Java of Delegate-Model architecture

JTree

<>

Javax.swing.tree.TreeNode

getChildAt(int index): Object getChildCount(): int getParent(): Treenode isLeaf(): boolean etc <>

Javax.swing.tree.TreeModel

getChild(Obj parent, int index): Object getIndexOfChild(Object): int getRoot(): Object isLeaf(Object node): boolean getRoot(TreeNode): Object <>

MutableTreeNode

Insert(MutableTreeNode chld, int idx) remove (int idx) etc DefaultTreeModel getPathToRoot(TreeNode) : TreeNode[] removeNodeFromParent ( MutableTreeNode node) void insertNodeInto( MutableTreeNode newChild, MutableTreeNode parent, int index)

DefaultMutableTreeNode

Constructor(Object o) add(MutableTreeNode chld) Insert(MutableTreeNode chld, int idx) remove (int idx) getDepth(): int breadthFirstEnumeration() :Enumeration depthFirstEnumeration() :Enumeration etc MVT Architecture Javax.swing.JComponent

JTree getLastSelectedPathComponent : Object 13

Examples in Java of Delegate-Model architecture

JTree

  A

TreeNode

is constructed from a given

Object

 Encapsulates the extra functionality for the object to be stored as a node of a tree DDS section 3.6.1 illustrates a tree with the

DefaultTreeModel

   The object stored at each node are

Strings

This application allows string object to to added as nodes and removed from the tree See listing of PhilosophersJTree.java

 DDS section 3.6.2 illustrates a custom (non-default) implementation of the

TreeModel

interface

public class FileSystemModel implements TreeModel ...

     Displays a disk file system (from some root directory) as a tree A

FileSystemModel File

object is constructed from a object as “root directory”.

It implements the

TreeModel

methods using

File

class methods of get information about a directory and its children (subdirectories & files) See listing of FileSystemModel.java

.

FileFreeFrame.java

is a simple application making use of a

FileSystemModel

.

MVT Architecture 14

Exercises

  Try each of the example programs out and make sure you understand them.

Do DDS exercises    3.6: Make

LiabilityPieChartView

as a subclass of

AssetPieChartView

that includes only

Account

s with negative balances. Modify class

AccountManager

to include a

LiabilityPieChartView

in addition to an

AssetPieChartView

3.7: Make a new version of

AccountBarGraphView

which shows multiple

Account

s in a single bar graph [Hint -- imitate

AssetPieChartView

] 3.8: Enhance 3.7 to allow transfers between accounts. Modify the

AccountController

to include a

JComboBox

to select the destination account, and a

JButton

to perform the transfer.

MVT Architecture 15