Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

jgs 460 00001100 The LEGO Paradigm

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

jgs 460 00001100 GoF Patterns

Slide 6

Slide 6 text

jgs 460 00001100

Slide 7

Slide 7 text

jgs 460 00001100 GoF Patterns

Slide 8

Slide 8 text

jgs Observer Software Design

Slide 9

Slide 9 text

jgs 460 00001100 Observer

Slide 10

Slide 10 text

jgs 460 00001100 Observer

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

jgs Java Implementation java.util.*

Slide 17

Slide 17 text

jgs 460 00001100 Scenario Student Tutor Classroom

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

jgs 460 00001100 Output

Slide 23

Slide 23 text

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.