Slide 1

Slide 1 text

jgs CSC 308 Software Engineering 1 Lecture 13: Design Patterns Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment

Slide 2

Slide 2 text

jgs Previously …

Slide 3

Slide 3 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 3 Not Everything that Can be … Should be …

Slide 4

Slide 4 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 4 From Assignment 01

Slide 5

Slide 5 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 5 Homework (1 of 2) Single Responsibility Principle

Slide 6

Slide 6 text

jgs Patterns Software Design

Slide 7

Slide 7 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 7 § 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). Definition

Slide 8

Slide 8 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 8 Timeline 1989 Beck OO Thinking 1993 Gamma et al. GoF Patterns

Slide 9

Slide 9 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 9 GoF Patterns

Slide 10

Slide 10 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 10 GoF Patterns

Slide 11

Slide 11 text

jgs Observer Software Design

Slide 12

Slide 12 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 12 From Assignment 01

Slide 13

Slide 13 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 13 Observer

Slide 14

Slide 14 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 14 class Student { public String answerQuestion (String question) { String answer; // solve the question return answer; } } Student

Slide 15

Slide 15 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 15 class Teacher { private String question; public String getQuestion() { return question; } public void createQuestion () { question = // AI to create a question here } } Teacher

Slide 16

Slide 16 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 16 Observer

Slide 17

Slide 17 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 17 public abstract class Observer { public abstract void update(Subject s); } Observer

Slide 18

Slide 18 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 18 import java.util.LinkedList; public abstract class Subject { private LinkedList observers = new LinkedList(); public void addObserver(Observer observer) { observers.add(observer); } public void removeObserver(Observer observer) { observers.remove(observer); } public void notifying() { for (Observer ob : observers) { ob.update(this); } } } Observable

Slide 19

Slide 19 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 19 class Student extends Observer { public String answerQuestion (String question) { String answer; // solve the question return answer; } @Override public void update(Observable s) { String q = ((Teacher)s).getQuestion(); Sting answer = answerQuestion (q); System.out.println (answer); } } Student as Observer

Slide 20

Slide 20 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 20 class Teacher extends Observable { private String question; public String getQuestion() { return question; } public void createQuestion () { question = // AI to create a question here notifying(); } } Teacher as Observable

Slide 21

Slide 21 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 21 public class Main { public static void main(String[]a) { Teacher teacher = new Teacher(); Student s1 = new Student(); Student s2 = new Student(); teacher.addObserver(s1); teacher.addObserver(s2); // Teaching teacher.createQuestion(); // More Teaching teacher.createQuestion(); } } Main

Slide 22

Slide 22 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 22 Scenario Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Teacher Student Main

Slide 23

Slide 23 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 23 Questions

Slide 24

Slide 24 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 24 Office Hours Tuesday and Thursday 3 - 5 pm But an appointment required Sent me an email – [email protected]

Slide 25

Slide 25 text

jgs

Slide 26

Slide 26 text

jgs CSC 308 Software Engineering 1 Lab 13: Assignment 02 Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment

Slide 27

Slide 27 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 27 Project – User create dots

Slide 28

Slide 28 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 28 Project – User selects Cluster and clicks Run ✅

Slide 29

Slide 29 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 29 Project – User selects Cluster and clicks Run ✅

Slide 30

Slide 30 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 30 Project – User selects Line and clicks Run ✅

Slide 31

Slide 31 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 31 Project – User selects Line and clicks Run ✅

Slide 32

Slide 32 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 32 Project – User selects Line and clicks Run ✅ ✅

Slide 33

Slide 33 text

jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 33 Project – User selects Cluster and clicks Run ✅ ✅

Slide 34

Slide 34 text

jgs CSC 308 Software Engineering I Javier Gonzalez-Sanchez, Ph.D. [email protected] Winter 2023 Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly. They cannot be distributed or used for another purpose.