Slide 1

Slide 1 text

Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info office: 14 -227 CSC 307 Introduction to Software Engineering Lecture 07. Patterns

Slide 2

Slide 2 text

Homework 1. Due Monday: Quiz 2

Slide 3

Slide 3 text

Homework Due Monday: Paint-App 2.0

Slide 4

Slide 4 text

Thread, Runnable, and Timer 4 https:/ /github.com/CSC3100/Pacman

Slide 5

Slide 5 text

Previously

Slide 6

Slide 6 text

Relationships 6 Association Directed Association Reflexive Association Multiplicity Aggregation Composition Generalization Realization

Slide 7

Slide 7 text

Good Code vs Bad Code 7

Slide 8

Slide 8 text

Not Everything that Can be … Should be … 8

Slide 9

Slide 9 text

Not Everything that Can be … Should be … 9

Slide 10

Slide 10 text

Not Everything that Can be … Should be … 10

Slide 11

Slide 11 text

Patterns

Slide 12

Slide 12 text

Definition • 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). 12

Slide 13

Slide 13 text

Timeline 13 1989 Beck OO Thinking 1993 Gamma et al. GoF Patterns

Slide 14

Slide 14 text

GoF Patterns 14

Slide 15

Slide 15 text

Observer

Slide 16

Slide 16 text

Observer 16

Slide 17

Slide 17 text

Classroom | Initial Version 17 https://github.com/CSC3100/Patterns

Slide 18

Slide 18 text

Student | Initial Version 18 https://github.com/CSC3100/Patterns

Slide 19

Slide 19 text

Teacher | Initial Version 19 https://github.com/CSC3100/Patterns

Slide 20

Slide 20 text

Observer 20

Slide 21

Slide 21 text

Observer 21 https://github.com/CSC3100/Patterns

Slide 22

Slide 22 text

Observable 22 https://github.com/CSC3100/Patterns

Slide 23

Slide 23 text

Classroom | New Version 23 https://github.com/CSC3100/Patterns

Slide 24

Slide 24 text

Student | New Version 24 https://github.com/CSC3100/Patterns

Slide 25

Slide 25 text

Teacher | New Version 25 https://github.com/CSC3100/Patterns

Slide 26

Slide 26 text

Important 26 Do not reinvent the wheel! (Do not make another implementation for classes Observer or Observable -they already exist in libraries!)

Slide 27

Slide 27 text

Important 27 Depreciated java.util.Observer java.util.Observable The implementation of these packages That is NOT about the Observer pattern being depreciated.

Slide 28

Slide 28 text

Important 28 Use java.beans.PropertyChangeListener java.beans.PropertyChangeSupport

Slide 29

Slide 29 text

Classroom | Listener Version 29 https://github.com/CSC3100/Patterns

Slide 30

Slide 30 text

Student | Listener Version 30 https://github.com/CSC3100/Patterns

Slide 31

Slide 31 text

Teacher | Listener Version 31 https://github.com/CSC3100/Patterns

Slide 32

Slide 32 text

Thoughts? 32

Slide 33

Slide 33 text

Test Yourselves

Slide 34

Slide 34 text

Problem • Imagine a class Teacher running in its own execution thread, forever grading—recording grades as a collection of numerical values. • A panel (PrinterPanel) would like to show the grades as they are generated. • Another panel (StatisticalPanel) would like to show real-time statistics about the grades (how many pass and how many fail). 34

Slide 35

Slide 35 text

Homework 35 Reading https://medium.com/swlh/software-design-patterns-why-what-and-an-example-8d4c0934a6ca

Slide 36

Slide 36 text

Singleton

Slide 37

Slide 37 text

Singleton 37

Slide 38

Slide 38 text

Singleton 38 https://github.com/CSC3100/Patterns

Slide 39

Slide 39 text

Singleton 39 https://github.com/CSC3100/Patterns

Slide 40

Slide 40 text

Warning 40 Do not Abuse of the Singleton You are transforming OOP into Structural Programming!! !

Slide 41

Slide 41 text

Note 41 Do you remember our class Officer

Slide 42

Slide 42 text

Homework 42 Reading https://medium.com/javarevisited/design-patterns-101-hello-singleton-2d3f227c7729

Slide 43

Slide 43 text

Decorator Composite

Slide 44

Slide 44 text

Decorator | Composite 44

Slide 45

Slide 45 text

Decorator | Composite 45

Slide 46

Slide 46 text

Decorator | Composite 46

Slide 47

Slide 47 text

Decorator 47

Slide 48

Slide 48 text

Composite 48

Slide 49

Slide 49 text

Example 49

Slide 50

Slide 50 text

Component 50 https://github.com/CSC3100/Patterns

Slide 51

Slide 51 text

ConcreteComponent | Ball 51 https://github.com/CSC3100/Patterns

Slide 52

Slide 52 text

Decorator 52 https://github.com/CSC3100/Patterns

Slide 53

Slide 53 text

ConcreteDecoratorOne | Bag 53 https://github.com/CSC3100/Patterns

Slide 54

Slide 54 text

ConcreteDecoratorTwo | Box 54 https://github.com/CSC3100/Patterns

Slide 55

Slide 55 text

Main 55 draw draw draw https://github.com/CSC3100/Patterns

Slide 56

Slide 56 text

Composite | Example 56 draw

Slide 57

Slide 57 text

Decorator vs Inheritance • Both allow you to change how an object behaves. • The decorator pattern allows you to extend (decorate) the functionality of an object at runtime. • Inheritance adds behavior at compilation time. 57

Slide 58

Slide 58 text

Homework 58 Reading https://medium.com/javarevisited/software-design-patterns-decorator-in-a-nutshell-eefd2025ea3c

Slide 59

Slide 59 text

Questions 59

Slide 60

Slide 60 text

Lab 07. Astah

Slide 61

Slide 61 text

Help me with the Class Diagram in Astah 62 https:/ /github.com/CSC3100/Pacman

Slide 62

Slide 62 text

Let’s Work 63

Slide 63

Slide 63 text

CSC 307 Introduction to Software Engineering Javier Gonzalez-Sanchez, Ph.D. [email protected] Summer 2024 Copyright. These slides can only be used as study material for the class CSC307 at Cal Poly. They cannot be distributed or used for another purpose.