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)

Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    Patterns Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. 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).
  3. 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); } }
  4. 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(); } } }
  5. jgs 460 00001100 ConcreteSubject class ConcreteSubject extends Subject { private

    String subjectState; public String getState () { return subjectState; } public void setState (s) { subjectState = s; } }
  6. 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(); } }
  7. jgs 460 00001100 Scenario Observer + update() Observable + addObserver()

    + notifyObservers() + setChanged() Tutor Student Classroom
  8. 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); } }
  9. 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; } }
  10. 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(); } }
  11. jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez [email protected]

    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.