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

CSE360 Lecture 18

CSE360 Lecture 18

Introduction to Software Engineering
Software Design Patterns: Observer
(201806)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE360
    Introduction to Software Engineering
    Lecture 18:
    Software Design Patterns: Observer II
    Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu
    Office Hours: By appointment

    View full-size slide

  2. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 2
    GoF Patterns
    Creational Patterns
    • Abstract Factory
    • Builder
    • Factory Method
    • Prototype
    • Singleton
    Structural Patterns
    • Adapter
    • Bridge
    • Composite
    • Decorator
    • Façade
    • Flyweight
    • Proxy
    Behavioral Patterns
    • Chain of Responsibility
    • Command
    • Interpreter
    • Iterator
    • Mediator
    • Memento
    • Observer
    • State
    • Strategy
    • Template Method
    • Visitor

    View full-size slide

  3. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 3
    Observer

    View full-size slide

  4. Java Implementation
    java.util.*

    View full-size slide

  5. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 5
    Java Subject (Observable)
    import java.util.Observable;
    public class ObservableDemo extends Observable {
    private String weather;
    public ObservableDemo(String weather) {
    this.weather = weather;
    }
    public String getWeather() {
    return weather;
    }
    public void setWeather(String weather) {
    this.weather = weather; setChanged();
    notifyObservers();
    }
    }

    View full-size slide

  6. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 6
    Java Observer (Observer)
    import java.util.Observable;
    import java.util.Observer;
    public class ObserverExample implements Observer {
    private ObservableDemo weatherUpdate ;
    @Override
    public void update(Observable observable, Object arg) {
    weatherUpdate = (ObservableDemo) observable;
    System.out.println("Weather Report Live. Its "+weatherUpdate.getWeather());
    }
    public static void main(String[] args) {
    ObservableDemo observable = new ObservableDemo(null);
    ObserverExample observer = new ObserverExample();
    observable.addObserver(observer);
    observable.setWeather("Bright and sunny... ");
    observable.setWeather("Raining Heavily!.. ");
    }
    }

    View full-size slide

  7. Everything you need to know
    for project 4 about Observer pattern.

    View full-size slide

  8. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 8
    Scenario
    Student Tutor
    Classroom

    View full-size slide

  9. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 9
    Scenario
    Observer
    + update()
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Tutor
    Student Classroom

    View full-size slide

  10. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 10
    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 full-size slide

  11. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 11
    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 full-size slide

  12. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 12
    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 full-size slide

  13. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 13
    Output

    View full-size slide

  14. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 14
    Scenario
    JFrame
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Tutor
    Student Classroom
    Observer
    + update()
    JPanel
    TutorPanel
    JPanel

    View full-size slide

  15. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 15
    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 full-size slide

  16. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 16
    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 full-size slide

  17. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 17
    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 full-size slide

  18. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 18
    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 full-size slide

  19. Javier Gonzalez-Sanchez | CSE360 | Summer 2017 | 19
    Output

    View full-size slide

  20. CSE360 – Introduction to Software Engineering
    Javier G onzalez-Sanchez
    javiergs@ asu.edu
    Sum m er 2017
    Disclaim er. These slides can only be used as study m aterial for the class C SE360 at ASU. They cannot be distributed or used for another purpose.

    View full-size slide