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

CSE360 Flipped Lecture 06

CSE360 Flipped Lecture 06

Introduction to Software Engineering
Objects and Patterns
(202010)

Javier Gonzalez-Sanchez
PRO

June 06, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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

    View Slide

  2. Key Ideas
    CSE360

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  9. 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?

    View Slide

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

    View Slide

  11. Next Step

    View Slide

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

    View Slide

  13. 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).

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. Observer Pattern
    Example with Java API

    View Slide

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

    View Slide

  28. 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);
    }
    }

    View Slide

  29. 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;
    }
    }

    View Slide

  30. 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();
    }
    }

    View Slide

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

    View Slide

  32. Decorator Pattern
    Example with Java

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  37. 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();
    }
    }

    View Slide

  38. 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. ");
    }
    }

    View Slide

  39. 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.");
    }
    }

    View Slide

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

    View Slide

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

    View Slide

  42. 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.

    View Slide