Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC308B Lecture 11

CSC308B Lecture 11

Software Engineering I
Patterns
(202401) - two-days per week version

Javier Gonzalez-Sanchez

March 01, 2024
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs CSC 308 Software Engineering 1 Lecture 11: Patterns Dr.

    Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227
  2. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    3 Homework (1 of 2) Single Responsibility Principle
  3. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    5 § 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
  4. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

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

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

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

    15 public abstract class Observer { public abstract void update(Subject s); } Observer
  8. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    16 import java.util.LinkedList; public abstract class Subject { private LinkedList<Observer> observers = new LinkedList<Observer>(); 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
  9. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    17 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
  10. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    18 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
  11. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    19 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
  12. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2024 |

    20 Scenario Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Teacher Student Main
  13. jgs

  14. jgs CSC 308 Software Engineering 1 Lab 11: Project Sprint

    2 (Continue) Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.com Building 14 -227 Office Hours: By appointment
  15. jgs CSC 308 Software Engineering I Javier Gonzalez-Sanchez, Ph.D. [email protected]

    Winter 2024 Copyright. These slides can only be used as study material for the class CSC 308 at Cal Poly. They cannot be distributed or used for another purpose.