Slide 1

Slide 1 text

Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227 CSC 305 Individual Software Design and Development Lecture 09. Patterns

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Standard Solutions Patterns

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

GoF Patterns 5

Slide 6

Slide 6 text

Observer Patterns

Slide 7

Slide 7 text

Scenario 7 Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Teacher Student Main

Slide 8

Slide 8 text

Observer public interface Observer { public abstract void update(Observable s); } 8

Slide 9

Slide 9 text

Observable import java.util.LinkedList; public class Observable { 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); } } } 9

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Teacher as Observable class Teacher extends Observable { private String question; public String getQuestion() { return question; } public void createQuestion () { // your code here notifying(); } } 11

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Test Yourselves Patterns

Slide 14

Slide 14 text

Lab. What about a new version for TicTaeToe 14 O O X

Slide 15

Slide 15 text

Questions 15

Slide 16

Slide 16 text

Lab

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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.