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

CSE460 Lecture 12

CSE460 Lecture 12

Software Analysis and Design
Design Patterns
(202102)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

July 12, 2020
Tweet

Transcript

  1. jgs CSE 460 Software Analysis and Design Lecture 12: Design

    Patterns Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs 460 00001100 Announcement § Quiz 04 is due Today

    (midnight) 11:59 PM AZ time.
  3. jgs 460 00001100 The LEGO Paradigm

  4. jgs 460 00001100 Patterns are these Blocks § 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 between objects. § The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns (Gamma, Helm, Johnson, and Vlissides).
  5. jgs 460 00001100 GoF Patterns

  6. jgs 460 00001100

  7. jgs 460 00001100 GoF Patterns

  8. jgs Observer Software Design

  9. jgs 460 00001100 Observer

  10. jgs 460 00001100 Observer

  11. jgs 460 00001100 Observer abstract class Observer { public abstract

    void update(); }
  12. jgs 460 00001100 ConcreteObserver class ConcreteObserver extends Observer { private

    string name; private string observerState; private ConcreteSubject subject; public Subject getSubject() { return subject; } public void setSubject(Subject s) { subject = s; } // 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); } }
  13. jgs 460 00001100 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(); } } }
  14. jgs 460 00001100 ConcreteSubject class ConcreteSubject extends Subject { private

    String subjectState; public String getState () { return subjectState; } public void setState (s) { subjectState = s; } }
  15. jgs 460 00001100 Main Public class Main { public static

    void Main() { // Configure Observer pattern ConcreteSubject s = new ConcreteSubject(); ConcreteObserver eyeOne = new ConcreteObserver(s, "X"); ConcreteObserver eyeTwo = new ConcreteObserver(s, "X"); s.Attach(eyeOne); s.Attach(eyeTwo); // Change subject and notify observers s.subjectState = "ABC"; s.notify(); } }
  16. jgs Java Implementation java.util.*

  17. jgs 460 00001100 Scenario Student Tutor Classroom

  18. jgs 460 00001100 Scenario Observer + update() Observable + addObserver()

    + notifyObservers() + setChanged() Tutor Student Classroom
  19. jgs 460 00001100 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); } }
  20. jgs 460 00001100 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. jgs 460 00001100 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(); } }
  22. jgs 460 00001100 Output

  23. jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez javiergs@asu.edu

    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.