Model View Controller - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Model View Controller

Description:

import javax.swing.JLabel; import javax.swing.JPanel; ... import javax.swing.JPanel; public class OvalViewOfColor extends JPanel implements Observer ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 15
Provided by: mer3
Category:

less

Transcript and Presenter's Notes

Title: Model View Controller


1
Model View Controller
  • Rick Mercer

2
Outline
  • Model View Controller
  • Observer as part of MVC
  • An example of the Observer Design Pattern

3
Model View Controller (MVC)
  • The intent of MVC is to keep neatly separate
    objects into one of tree categories
  • Model
  • The data, the business logic, rules, strategies,
    and so on
  • View
  • Displays the model and usually has components
    that allows user to edit change the model
  • Controller
  • Allows data to flow between the view and the
    model
  • The controller mediates between the view and model

4
Model
  • The Model's responsibilities
  • Provide access to the state of the system
  • Provide access to the system's functionality
  • Can notify the view(s) that its state has changed

5
View
  • The view's responsibilities
  • Display the state of the model to the user
  • At some point, the model (a.k.a. the observable)
    must registers the views (a.k.a. observers) so
    the model can notify the observers that its state
    has changed

6
Controller
  • The controller's responsibilities
  • Accept user input
  • Button clicks, key presses, mouse movements,
    slider bar changes
  • Send messages to the model, which may in turn
    notify it observers
  • Send appropriate messages to the view
  • In Java, listeners are controllers

7
from http//www.enode.com/x/markup/tutorial/mvc.ht
ml)
8
MVC Misunderstood
  • MVC is understood by different people in
    different ways
  • It is often misunderstood, but most software
    developers will say it is important powerful
  • Lets start it right MVC is a few patterns put
    together
  • Consider the Observer Pattern

9
Compound Patterns
  • The observer pattern is part of MVC
  • In Java, you can use Observer support
  • The model extends Observable
  • view implements Observer (one method update)
  • You need to addObservers to the model
  • The model sends these two messages when it thinks
    the view should be updated
  • setChanged()
  • notifyObservers()

10
Model
  • import java.awt.Color
  • import java.util.Observable
  • import java.util.Random
  • public class Model extends Observable
  • private Color currentColor
  • private static Random generator // Or set up
    any color
  • public Model()
  • generator new Random()
  • currentColor Color.PINK
  • generator.nextInt(256) 25,
    generator.nextInt(256)39)
  • public void changeColor()
  • // Make the colors change seemingly randomly
  • int red (currentColor.getRed()
    generator.nextInt(256)) 256
  • int green (currentColor.getGreen()
    generator.nextInt(256)) 256
  • int blue (currentColor.getRed()
    generator.nextInt(256)) 256

11
One View
  • import java.awt.Color
  • import java.util.Observable
  • import java.util.Observer
  • import javax.swing.JLabel
  • import javax.swing.JPanel
  • public class TextViewOfColor extends JPanel
    implements Observer
  • private Color color
  • private JLabel label
  • public TextViewOfColor(Color color)
  • this.color color
  • label new JLabel()
  • label.setText("A text 'view' of color "
    color.toString())
  • this.setBackground(Color.CYAN)
  • this.add(label)

12
Controller
  • import java.awt.
  • import javax.swing.
  • public class Controller extends JFrame
  • public static void main(String args)
  • Controller controller new Controller()
  • controller.setVisible(true)
  • private Model model
  • private OvalViewOfColor view
  • private TextViewOfColor view2
  • private JButton startButton new
    JButton("Change Color")
  • public Controller()
  • setSize(450, 250)
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • Container cp getContentPane()

13
The Model
  • public void setUpModelAndObservers()
  • model new Model()
  • model.changeColor()
  • // Register the observers (views) to be
    notified of state changes
  • // when the observed object (model) changes
    its state and notifies observers
  • view2 new TextViewOfColor(Color.PINK)
  • add(view2, BorderLayout.NORTH)
  • model.addObserver(view2)
  • view new OvalViewOfColor(Color.PINK)
  • getContentPane().add(view, BorderLayout.CENTER
    )
  • model.addObserver(view)
  • private class ButtonListener implements
    ActionListener
  • public void actionPerformed(ActionEvent arg0)
  • model.changeColor()

14
Another View
  • import java.awt.
  • import java.util.Observable
  • import java.util.Observer
  • import javax.swing.JPanel
  • public class OvalViewOfColor extends JPanel
    implements Observer
  • Color color
  • public OvalViewOfColor(Color color)
  • this.color color
  • _at_Override
  • public void paintComponent(Graphics g)
  • Graphics2D g2 (Graphics2D) g
  • super.paintComponent(g2)
  • g2.setColor(color)
Write a Comment
User Comments (0)
About PowerShow.com