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

CSE360 Tutorial 06

CSE360 Tutorial 06

Introduction to Software Engineering
Observer
(202206)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

June 15, 2022
Tweet

More Decks by Javier Gonzalez

Other Decks in Programming

Transcript

  1. CSE360 Introduction to Software Engineering Lecture: Software Design Patterns -

    Observer Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  2. Previously …

  3. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 3 The

    SOLID Principles
  4. Design Patterns

  5. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 5 Definition

    • Design patterns are solutions to software design problems you find again and again in real-world application development. • Patterns are about reusable designs and interactions of objects. • The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns (Gamma, Helm, Johnson, and Vlissides).
  6. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 6 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
  7. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 7 Observer

  8. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 8 Observer

    Public class MainApp{ public static void Main() { // Configure Observer pattern ConcreteSubject s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s, "X")); s.Attach(new ConcreteObserver(s, "Y")); s.Attach(new ConcreteObserver(s, "Z")); // Change subject and notify observers s.SubjectState = "ABC"; s.Notify(); } }
  9. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 9 Subject

    abstract class Subject { private List<Observer> _observers=new List<Observer>(); public void Attach(Observer observer) { _observers.Add(observer); } public void Detach(Observer observer){ _observers.Remove(observer); } public void Notify(){ foreach (Observer o in _observers){ o.Update(); } } }
  10. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 10 ConcreteSubject

    class ConcreteSubject extends Subject { private String _subjectState; public String getState { return _subjectState; } public void setState (value) { _subjectState = value; } }
  11. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 11 Observer

    abstract class Observer { public abstract void update(); }
  12. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 12 ConcreteObserver

    class ConcreteObserver extends Observer { private string _name; private string _observerState; private ConcreteSubject _subject; // Constructor public ConcreteObserver(ConcreteSubject s, string n){ this._subject = s; this._name = n; } public override void update() { _observerState = _subject.SubjectState; println("Observer %s's new state is $s", _name, _observerState); } public Subject getSubject() { return _subject; } public void setSubject(Subject value) { _subject = value; } }
  13. Java Implementation java.util.*

  14. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 14 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(); } }
  15. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 15 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!.. "); } }
  16. Example I

  17. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 17 Scenario

    Student Tutor Classroom
  18. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 18 Scenario

    Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom
  19. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 19 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(); } }
  20. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 20 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; } }
  21. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 21 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); } }
  22. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 22 Output

  23. Example II

  24. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 24 Scenario

    JFrame Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom Observer + update() JPanel TutorPanel JPanel
  25. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 25 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); } }
  26. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 26 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; } }
  27. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 27 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()); } }
  28. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 28 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); } }
  29. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 29 Output

  30. Javier Gonzalez-Sanchez | CSE360 | Summer 2018 | 30 Assignment

    02
  31. CSE360 – Introduction to Software Engineering Javier Gonzalez-Sanchez javiergs@asu.edu Summer

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