$30 off During Our Annual Pro Sale. View Details »

CSE460 Lecture 12

CSE460 Lecture 12

Software Analysis and Design
Design Patterns
(202102)

Javier Gonzalez-Sanchez
PRO

July 12, 2020
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

    View Slide

  2. jgs
    460 00001100
    Announcement
    § Quiz 04 is due Today (midnight) 11:59 PM AZ time.

    View Slide

  3. jgs
    460 00001100
    The LEGO Paradigm

    View Slide

  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).

    View Slide

  5. jgs
    460 00001100
    GoF Patterns

    View Slide

  6. jgs
    460 00001100

    View Slide

  7. jgs
    460 00001100
    GoF Patterns

    View Slide

  8. jgs
    Observer
    Software Design

    View Slide

  9. jgs
    460 00001100
    Observer

    View Slide

  10. jgs
    460 00001100
    Observer

    View Slide

  11. jgs
    460 00001100
    Observer
    abstract class Observer {
    public abstract void update();
    }

    View Slide

  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);
    }
    }

    View Slide

  13. jgs
    460 00001100
    Subject
    abstract class Subject {
    private List observers = new List();
    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();
    }
    }
    }

    View Slide

  14. jgs
    460 00001100
    ConcreteSubject
    class ConcreteSubject extends Subject {
    private String subjectState;
    public String getState () {
    return subjectState;
    }
    public void setState (s) {
    subjectState = s;
    }
    }

    View Slide

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

    View Slide

  16. jgs
    Java Implementation
    java.util.*

    View Slide

  17. jgs
    460 00001100
    Scenario
    Student Tutor
    Classroom

    View Slide

  18. jgs
    460 00001100
    Scenario
    Observer
    + update()
    Observable
    + addObserver()
    + notifyObservers()
    + setChanged()
    Tutor
    Student Classroom

    View Slide

  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);
    }
    }

    View Slide

  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;
    }
    }

    View Slide

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

    View Slide

  22. jgs
    460 00001100
    Output

    View Slide

  23. 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.

    View Slide