Slide 1

Slide 1 text

CSE 360 Introduction to Software Engineering Lecture 06: Objects and Patterns Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Key Ideas CSE360

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 3 Key Concepts Encapsulation Abstraction Hierarchical Relationships Polymorphism Concurrency Persistency

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 4 Abstraction • Reducing by focusing on the essential observable behavior • What will be outside? OOAD, Booch Textbook

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 5 Encapsulation • Hiding the details of the implementation • What is inside? OOAD, Booch Textbook

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 6 Hierarchical Relationships • Creating a hierarchy of abstractions • Who (is a…, uses a…, has a…) ? OOAD, Booch Textbook

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 7 Hierarchical Relationships Association Aggregation Composition Generalization Realization

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 8 8 Hierarchical Relationships

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 9 Polymorphism • The condition of denoting different forms. • A single name may denote objects of many different classes that are related by some common superclass. • When to use it?

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 10 High cohesion Low coupling Important

Slide 11

Slide 11 text

Next Step

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 12 The LEGO Paradigm

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 13 Patterns are these Blocks • 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).

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 14 GoF Patterns

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 15 Blueprint

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 16 GoF Patterns

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 17 Observer

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 18 Observer

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 19 Observer Java Listeners

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 20 GoF Patterns

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 21 Decorator

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 22 Examples

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 23 Decorator

Slide 24

Slide 24 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 24 Examples Java UI Objects

Slide 25

Slide 25 text

To be continued… Review the code in the slides at the end with examples of Observer and Decorator

Slide 26

Slide 26 text

Observer Pattern Example with Java API

Slide 27

Slide 27 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 27 Scenario Observer + update() Observable + addObserver() + notifyObservers() + setChanged() Tutor Student Classroom

Slide 28

Slide 28 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 28 Student 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); } }

Slide 29

Slide 29 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 29 Tutor 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; } }

Slide 30

Slide 30 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 30 Classroom 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(); } }

Slide 31

Slide 31 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 31 Output

Slide 32

Slide 32 text

Decorator Pattern Example with Java

Slide 33

Slide 33 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 33 Decorator

Slide 34

Slide 34 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 34 Main

Slide 35

Slide 35 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 35 Companion public interface Companion { public void doSomething(); }

Slide 36

Slide 36 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 36 BasicCompanion public class BasicCompanion implements Companion { @Override public void doSomething() { System.out.print("Hello Student, "); } }

Slide 37

Slide 37 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 37 CompanionDecorator public class CompanionDecorator implements Companion { protected Companion c; public void add(Companion c){ this.c = c; } @Override public void doSomething() { this.c.doSomething(); } }

Slide 38

Slide 38 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 38 HelperCompanion public class HelperCompanion extends CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to help you. "); } }

Slide 39

Slide 39 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 39 AffectiveCompanion public class AffectiveCompanion extends CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to cheer you."); } }

Slide 40

Slide 40 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 40 Homework Complete This Week’s Hybrid Activities

Slide 41

Slide 41 text

Javier Gonzalez-Sanchez | CSE360 | Fall 2020 | 41 References Chapter 5

Slide 42

Slide 42 text

CSE360 – Introduction to Software Engineering Javier Gonzalez-Sanchez [email protected] Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE360 at ASU. They cannot be distributed or used for another purpose.