CSE460 Lecture 16

CSE460 Lecture 16

Software Analysis and Design
Design Patterns
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

July 16, 2020
Tweet

Transcript

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

    Patterns Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 2 jgs

    The LEGO Paradigm
  3. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 3 jgs

    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).
  4. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 4 jgs

    GoF Patterns
  5. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 5 jgs

  6. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 6 jgs

    GoF Patterns
  7. jgs Observer Software Design

  8. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 8 jgs

    Observer
  9. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 9 jgs

    Observer
  10. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 10 jgs

    Observer abstract class Observer { public abstract void update(); }
  11. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 11 jgs

    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); } }
  12. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 12 jgs

    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(); } } }
  13. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 13 jgs

    ConcreteSubject class ConcreteSubject extends Subject { private String subjectState; public String getState () { return subjectState; } public void setState (s) { subjectState = s; } }
  14. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 14 jgs

    Observer 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(); } }
  15. jgs Java Implementation To be continued…

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