Slide 1

Slide 1 text

jgs CSE 564 Software Design Lecture 10: Design Patterns - Observer Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Previously

Slide 3

Slide 3 text

jgs 564 00001001 Observer

Slide 4

Slide 4 text

jgs 564 00001001 Subject import java.util.LinkedList; public abstract class Subject { private LinkedList observers = new LinkedList(); public void addObserver(Observer observer) { observers.add(observer); } public void removeObserver(Observer observer) { observers.remove(observer); } public void notifying() { for (Observer ob : observers) { ob.update(this); } } }

Slide 5

Slide 5 text

jgs 564 00001001 Observer public abstract class Observer { public abstract void update(Subject s); }

Slide 6

Slide 6 text

jgs 564 00001001 ConcreteSubject class ConcreteSubject extends Subject { private String data; public String getData() { return data; } public void setData(String s) { data = s; notifying(); } }

Slide 7

Slide 7 text

jgs 564 00001001 ConcreteObserver class ConcreteObserver extends Observer { @Override public void update(Subject s) { String info = ((ConcreteSubject)s).getData(); System.out.println("Subject is doing this: "+info); } }

Slide 8

Slide 8 text

jgs 564 00001001 Main public class Main { public static void main(String[]a) { // Configure pattern ConcreteSubject s = new ConcreteSubject(); ConcreteObserver eyeOne = new ConcreteObserver(); ConcreteObserver eyeTwo = new ConcreteObserver(); s.addObserver(eyeOne); s.addObserver(eyeTwo); // Change subject s.setData("ABC"); s.setData("XYZ"); } }

Slide 9

Slide 9 text

jgs 564 00001001 Main public class Main { public static void main(String[]a) { // Configure pattern ConcreteSubject s = new ConcreteSubject(); ConcreteObserver eyeOne = new ConcreteObserver(); ConcreteObserver eyeTwo = new ConcreteObserver(); s.addObserver(eyeOne); s.addObserver(eyeTwo); // Change subject s.setData("ABC"); s.setData("XYZ"); } }

Slide 10

Slide 10 text

jgs Java Implementation java.util.*

Slide 11

Slide 11 text

jgs 564 00001001 Note § Java 9 came out, and deprecated Observer implementation in java.util package. Deprecated their own implementation of the concept, not the concept itself. 🤔 § Problems: serialization and thread safety problems § Optional solution PropertyChangeListener from java.beans package.

Slide 12

Slide 12 text

jgs 564 00001001 Scenario Student Tutor Classroom

Slide 13

Slide 13 text

jgs 564 00001001 Scenario Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom

Slide 14

Slide 14 text

jgs 564 00001001 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); } }

Slide 15

Slide 15 text

jgs 564 00001001 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; } }

Slide 16

Slide 16 text

jgs 564 00001001 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(); } }

Slide 17

Slide 17 text

jgs 564 00001001 Output

Slide 18

Slide 18 text

jgs Example 2 Observer

Slide 19

Slide 19 text

jgs 564 00001001 Output

Slide 20

Slide 20 text

jgs 564 00001001 Scenario JFrame Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom Observer + update() JPanel TutorPanel JPanel

Slide 21

Slide 21 text

jgs 564 00001001 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); } }

Slide 22

Slide 22 text

jgs 564 00001001 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; } }

Slide 23

Slide 23 text

jgs 564 00001001 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()); } }

Slide 24

Slide 24 text

jgs 564 00001001 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); } }

Slide 25

Slide 25 text

jgs 564 00001001 Questions about Observer

Slide 26

Slide 26 text

jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.