CSE564 Lecture 08

CSE564 Lecture 08

Software Design
Design Patterns: Observer
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

August 08, 2020
Tweet

Transcript

  1. jgs CSE 564 Software Design Lecture 08: 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 | CSE564 | Fall 2020 | 2 jgs

    Reading § Design Patterns: Abstraction and Reuse of Object-Oriented Design Gamma E, Helm R, Johnson R, Vlissides J 1993 ECOOP § An Introduction to Software Architecture Garlan D, Shaw M 1996 CMU-CS
  3. jgs Previously Object-Oriented Design and Programming

  4. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 4 jgs

    Object Oriented World | Relationships Association Directed Association Reflexive Association Multiplicity Aggregation Composition Generalization Realization
  5. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 5 jgs

    Not Everything that Can be … Should be …
  6. jgs Patterns For Software Design

  7. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 7 jgs

    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 between objects. § The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns (Gamma, Helm, Johnson, and Vlissides).
  8. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 8 jgs

    Timeline 1989 Beck OO Thinking Software Architecture 1993 Gamma et al. GoF Patterns 1994 Garlan
  9. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 9 jgs

    GoF Patterns
  10. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 10 jgs

    GoF Patterns
  11. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 11 jgs

    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
  12. jgs Observer Pattern Software Design

  13. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 13 jgs

    Observer
  14. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 14 jgs

    Observer
  15. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 15 jgs

    Observer abstract class Observer { public abstract void update(); }
  16. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 16 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); } }
  17. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 17 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(); } } }
  18. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 18 jgs

    ConcreteSubject class ConcreteSubject extends Subject { private String subjectState; public String getState () { return subjectState; } public void setState (s) { subjectState = s; } }
  19. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 19 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(); } }
  20. jgs Java Implementation java.util.*

  21. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 21 jgs

    Scenario Student Tutor Classroom
  22. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 22 jgs

    Scenario Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom
  23. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 23 jgs

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

    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; } }
  25. Javier Gonzalez-Sanchez | CSE564 | Fall 2020 | 25 jgs

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

    Output
  27. jgs CSE 564 Computer Systems Fundamentals Javier Gonzalez-Sanchez javiergs@asu.edu Fall

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