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

CSC305 Lecture 09

CSC305 Lecture 09

Individual Software Design and Development
Patterns
(202410)

Javier Gonzalez-Sanchez

October 10, 2024
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227

    CSC 305 Individual Software Design and Development Lecture 09. Patterns
  2. De f inition • Design patterns a re solutions to

    softw a re design problems you f ind a g a in a nd a g a in in re a l-world a pplic a tion development. • P a tterns a re a bout reusable designs a nd inter a ctions between objects. • The 23 G a ng of Four (GoF) patterns a re gener a lly considered the found a tion for a ll other p a tterns (G a mm a , Helm, Johnson, a nd Vlissides). 4
  3. Observable import java.util.LinkedList; public class Observable { 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); } } } 9
  4. Student as Observer 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); } } 10
  5. Teacher as Observable class Teacher extends Observable { private String

    question; public String getQuestion() { return question; } public void createQuestion () { // your code here notifying(); } } 11
  6. Main 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(); } } 12
  7. Lab

  8. Lab. What about a new version for TicTaeToe 1. Cre

    a te a Jfr a me a nd a Jp a nel 3. Dr a w the 9 boxes to represent the TicT a cToe 5. Add MouseListeners to detect clicks on the P a nel 7. Review if the click is inside of a box 9. If so, dr a w a nd X or a 0 on th a t position (one time X, the next O, the next X, etc) Do not worry a bout the g a me (Tic-T a c-Toe) logic, yet. The go a l of this l a b is just a GUI with listeners 17
  9. CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D.

    [email protected] Winter 2025 Copyright. These slides can only be used as study material for the class CSC305 at Cal Poly. They cannot be distributed or used for another purpose.