$30 off During Our Annual Pro Sale. View Details »

CSE460 Lecture 13

CSE460 Lecture 13

Software Analysis and Design
Observer
(202102)

Javier Gonzalez-Sanchez
PRO

July 13, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 460
    Software Analysis and Design
    Lecture 13: Observer
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Previously …
    Design Patterns

    View Slide

  3. jgs
    00000001
    GoF Patterns

    View Slide

  4. jgs
    00000001
    Observer

    View Slide

  5. jgs
    00000001
    Observer

    View Slide

  6. jgs
    Java Implementation
    java.util.*

    View Slide

  7. jgs
    00000001
    Scenario
    Observer
    + update()
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Tutor
    Student Classroom

    View Slide

  8. jgs
    00000001
    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);
    }
    }

    View Slide

  9. jgs
    00000001
    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;
    }
    }

    View Slide

  10. jgs
    00000001
    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();
    }
    }

    View Slide

  11. jgs
    00000001
    Output

    View Slide

  12. jgs
    Example 2
    Observer

    View Slide

  13. jgs
    00000001
    Scenario
    JFrame
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Tutor
    Student Classroom
    Observer
    + update()
    JPanel
    TutorPanel
    JPanel

    View Slide

  14. jgs
    00000001
    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);
    }
    }

    View Slide

  15. jgs
    00000001
    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;
    }
    }

    View Slide

  16. jgs
    00000001
    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());
    }
    }

    View Slide

  17. jgs
    00000001
    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);
    }
    }

    View Slide

  18. jgs
    00000001
    Output

    View Slide

  19. jgs
    Example 3
    Observer

    View Slide

  20. jgs
    00000001
    Observer - Steps

    View Slide

  21. jgs
    00000001
    Source

    View Slide

  22. jgs
    00000001
    Boss

    View Slide

  23. jgs
    00000001
    NoteTaker

    View Slide

  24. jgs
    00000001
    World

    View Slide

  25. jgs
    CSE 460 Software Analysis and Design
    Javier Gonzalez-Sanchez
    [email protected]
    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.

    View Slide