Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC308 Lecture 14

CSC308 Lecture 14

Software Engineering I
Design Patterns - Observer
(202302)

Javier Gonzalez-Sanchez
PRO

October 26, 2022
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSC 308
    Software Engineering 1
    Lecture 14:
    Design Patterns - Observer
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227

    View Slide

  2. jgs
    Previously …

    View Slide

  3. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 3
    GoF Patterns

    View Slide

  4. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 4
    Scenario
    Observer
    + update()
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Teacher
    Student Main

    View Slide

  5. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 5
    public class Main {
    public static void main(String[]a) {
    Teacher teacher1 = new Teacher();
    Teacher teacher2 = new Teacher();
    Student s1 = new Student();
    Student s2 = new Student();
    teacher1.addObserver(s1);
    teacher2.addObserver(s2);
    teacher1.addObserver(s1);
    teacher2.addObserver(s2);
    teacher1.createQuestion();
    teacher2.createQuestion();
    }
    }
    Scenario

    View Slide

  6. jgs
    Java Implementation
    java.util.*

    View Slide

  7. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 7
    Scenario
    Observer
    + update()
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Teacher
    Student Main

    View Slide

  8. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 8
    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);
    }
    }
    Student

    View Slide

  9. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 9
    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;
    }
    }
    Tutor

    View Slide

  10. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 10
    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();
    }
    }
    Classroom

    View Slide

  11. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 11
    Output

    View Slide

  12. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 12
    Java API
    Java 9 deprecated Observer and Observable
    implementation in java.util package.
    It deprecated its own implementation of the
    concept, not the concept itself
    Why? serialization and thread safety problems.
    Solution: PropertyChangeListener from
    java.beans package.

    View Slide

  13. jgs
    Test Yourselves

    View Slide

  14. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 14
    Output

    View Slide

  15. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 15
    Scenario A
    JFrame
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Tutor
    Student Classroom
    Observer
    + update()
    JPanel
    TutorPanel
    JPanel

    View Slide

  16. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 16
    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);
    }
    }
    Student (version 2)

    View Slide

  17. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 17
    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;
    }
    }
    Tutor (version 2)

    View Slide

  18. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 18
    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());
    }
    }
    TutorPanel

    View Slide

  19. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 19
    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);
    }
    }
    Classroom (version 2)

    View Slide

  20. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 20
    Scenario B
    JFrame
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    TutorBrain
    StudentBrain Classroom
    Observer
    + update()
    JPanel
    TutorPanel
    JPanel
    StudentPanel

    View Slide

  21. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 21
    Scenario

    View Slide

  22. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 22
    Questions

    View Slide

  23. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 23
    Office Hours
    Tuesday and Thursday 3 - 5 pm
    But an appointment required
    Sent me an email – [email protected]

    View Slide

  24. jgs

    View Slide

  25. jgs
    CSC 308
    Software Engineering 1
    Lab 14:

    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  26. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 26
    Ideas
    No Lab Today
    But you have a Quiz instead.
    It is due today before midnight.

    View Slide

  27. jgs
    CSC 308 Software Engineering I
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Winter 2023
    Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly.
    They cannot be distributed or used for another purpose.

    View Slide