CSE460 Lecture 17

CSE460 Lecture 17

Software Analysis and Design
Observer
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

July 17, 2020
Tweet

Transcript

  1. jgs CSE 460 Software Analysis and Design Lecture 17: Observer

    Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Previously … Design Patterns

  3. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 3 jgs

    GoF Patterns
  4. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 4 jgs

    Observer
  5. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 5 jgs

    Observer
  6. jgs Java Implementation java.util.*

  7. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 7 jgs

    Scenario Student Tutor Classroom
  8. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 8 jgs

    Scenario Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom
  9. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 9 jgs

    Student import java.util.Observable; import java.util.Observer; public class Student implements Observer { public String answerQuestion (String question) { return "I am thinking about \'" + question +"\'"; } @Override public void update(Observable o, Object arg) { String x = ((Tutor)o).getQuestion(); String y = this.answerQuestion(x); System.out.println(y); } }
  10. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 10 jgs

    Tutor import java.util.Observable; public class Tutor extends Observable { private String [] questions = { "2 + 2", "public or private", "functional or not fuctional", "white or black" }; private String theQuestion; public void askQuestion() { theQuestion = questions[((int)(Math.random()*10))%4]; setChanged(); notifyObservers(); } public String getQuestion() { return theQuestion; } }
  11. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 11 jgs

    Classroom public class Classroom { public static void main(String[] args) { Student student = new Student(); Tutor tutor = new Tutor(); tutor.addObserver(student); for (int i=0; i<5;i++) tutor.askQuestion(); } }
  12. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 12 jgs

    Output
  13. jgs Example 2 Observer

  14. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 14 jgs

    Scenario JFrame Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom Observer + update() JPanel TutorPanel JPanel
  15. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 15 jgs

    Student (version 2) import java.util.*; import javax.swing.*; public class Student extends JPanel implements Observer { JLabel label = new JLabel(); public Student () { this.setBackground(Color.lightGray); ImageIcon background = new ImageIcon("src/student.png"); label.setIcon(background); add(label); } public String answerQuestion (String question) { return "I am thinking about \'" + question +"\'"; } @Override public void update(Observable o, Object arg) { String x = ((Tutor)o).getQuestion(); String y = this.answerQuestion(x); label.setText(y); } }
  16. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 16 jgs

    Tutor (version 2) import java.util.Observable; public class Tutor extends Observable { private String theQuestion; public void askQuestion() { theQuestion = questions[((int)(Math.random()*10))%4]; setChanged(); notifyObservers(); } public void askQuestion(String s) { theQuestion = s; setChanged(); notifyObservers(); } public String getQuestion() { return theQuestion; } }
  17. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 17 jgs

    TutorPanel import javax.swing.*; import javax.awt.*; public class TutorPanel extends JPanel implements ActionListener{ JTextField question = new JTextField(15); JButton ok = new JButton("send"); Tutor tutor; public TutorPanel(Tutor tutor) { this.tutor = tutor; setBackground(Color.white); ImageIcon background = new ImageIcon("src/prof.png"); JLabel label = new JLabel(); label.setIcon(background); add (question); add (ok); ok.addActionListener(this); add(label); } @Override public void actionPerformed(ActionEvent e) { tutor.askQuestion(question.getText()); } }
  18. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 18 jgs

    Classroom (version 2) import java.awt.GridLayout; import javax.swing.JFrame; public class Classroom extends JFrame { public Classroom() { Student student = new Student (); Tutor tutor = new Tutor(); tutor.addObserver(student); setLayout(new GridLayout(2,1)); add(student); add(new TutorPanel(tutor)); } public static void main(String[] args) { JFrame window = new Classroom(); window.setSize(500, 500); window.setVisible(true); } }
  19. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 19 jgs

    Output
  20. jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez javiergs@asu.edu

    Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE460 at ASU. They cannot be distributed or used for another purpose.