$30 off During Our Annual Pro Sale. View Details »

CSC307 Lecture 10

CSC307 Lecture 10

Introduction to Software Engineering
Design Patterns I
(202307)

Javier Gonzalez-Sanchez
PRO

July 09, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    View Slide

  2. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 3
    public class A extends B {
    C c1, c2;
    public A() {
    c1 = new C();
    }
    public void method() {
    D d = new D();
    d.working();
    }
    }
    Public class X {
    public void m() {
    B var = new A();
    double x = Math.sqrt(5);
    }
    }
    public class B implements E {
    public B() {
    C c1 = new C();
    }
    public void method() {
    B b = new B();
    b.sleep();
    }
    }
    public class Y {
    A [] a = new A[5];
    }
    Review

    View Slide

  3. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 4
    Quiz 02

    View Slide

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

    View Slide

  5. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 6
    Assignment 01
    https://blockly-spiderworld.sourceforge.io/appengine/spider.html

    View Slide

  6. jgs
    Design

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 11
    App

    View Slide

  11. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 12
    Class Diagram

    View Slide

  12. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 13
    Key Idea
    Single Responsibility
    Principle

    View Slide

  13. jgs
    Observer

    View Slide

  14. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 15
    Observer

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  18. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 19
    public abstract class Observer {
    public abstract void update(Observable s);
    }
    Observer

    View Slide

  19. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 20
    import java.util.LinkedList;
    public abstract 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);
    }
    }
    }
    Observable

    View Slide

  20. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 21
    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

    View Slide

  21. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 22
    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

    View Slide

  22. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 23
    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

    View Slide

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

    View Slide

  24. jgs
    Java Implementation
    java.util.*

    View Slide

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

    View Slide

  26. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 27
    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);
    }
    }
    Student

    View Slide

  27. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 28
    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;
    }
    }
    Tutor

    View Slide

  28. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 29
    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();
    }
    }
    Classroom

    View Slide

  29. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 30
    Output

    View Slide

  30. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 31
    Java API
    Java 9 deprecated Observer and Observable
    implementation in java.util package.
    It deprecated its own implementation of the
    concept, not the concept itself
    Why? serialization and thread safety problems.
    Solution: PropertyChangeListener from
    java.beans package.

    View Slide

  31. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 61
    Questions

    View Slide

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

    View Slide

  33. jgs

    View Slide

  34. jgs
    CSC 307
    Introduction to Software Engineering
    Lab 10:
    Design Patterns
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  35. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 65
    Scenario

    View Slide

  36. jgs
    CSC 307 Introduction to Software Engineering
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Summer 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.

    View Slide