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

CSC307 Lecture 12

CSC307 Lecture 12

Introduction to Software Engineering
Design Patterns III
(202307)

Javier Gonzalez-Sanchez
PRO

July 11, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSC 307
    Introduction to Software Engineering
    Lecture 12:
    Design Patterns III
    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
    GoF Patterns

    View Slide

  3. jgs
    Decorator & Composite
    Design Patterns

    View Slide

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

    View Slide

  5. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 6
    Decorator

    View Slide

  6. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 7
    Decorator

    View Slide

  7. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 8
    abstract class Component {
    public abstract void operation();
    }
    Component

    View Slide

  8. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 9
    class ConcreteComponent extends Component {
    @override
    public void operation() {
    System.out.print("ConcreteComponent-Operation()");
    }
    }
    ConcreteComponent

    View Slide

  9. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 10
    abstract class Decorator extends Component {
    protected Component component;
    public void setComponent(Component component) {
    this.component = component;
    }
    @override
    public void operation() {
    if (component != null) {
    component.operation();
    }
    }
    }
    Decorator

    View Slide

  10. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 11
    class ConcreteDecoratorA extends Decorator {
    @override
    public void operation() {
    super.operation();
    System.out.println("ConcreteDecoratorA-Operation()”);
    }
    }
    ConcreteDecoratorA

    View Slide

  11. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 12
    class ConcreteDecoratorB extends Decorator {
    @override
    public void operation() {
    super.operation();
    addedBehavior();
    System.out.println("ConcreteDecoratorB-Operation()");
    }
    void addedBehavior() {
    }
    }
    ConcreteDecoratorB

    View Slide

  12. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 13
    class MainApp {
    static void main() {
    // Create ConcreteComponent and two Decorators
    ConcreteComponent c = new ConcreteComponent();
    ConcreteDecoratorA d1 = new ConcreteDecoratorA();
    ConcreteDecoratorB d2 = new ConcreteDecoratorB();
    // Link decorators
    d1.SetComponent(c);
    d2.SetComponent(d1);
    d2.Operation();
    }
    }
    Main

    View Slide

  13. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 14
    class MainApp {
    static void main() {
    // Create ConcreteComponent and two Decorators
    ConcreteComponent c = new ConcreteComponent();
    ConcreteDecoratorA d1 = new ConcreteDecoratorA();
    ConcreteDecoratorB d2 = new ConcreteDecoratorB();
    // Link decorators
    d1.SetComponent(c);
    d2.SetComponent(d1);
    d2.Operation();
    }
    }
    Main
    ConcreteComponent - Operation()
    ConcreteDecoratorA - Operation()
    ConcreteDecoratorB - Operation()

    View Slide

  14. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 15
    § Both allow you to change how an object behaves
    § The decorator pattern can be used to make it possible to extend (decorate)
    the functionality of a certain object at runtime.
    § Inheritance adds behavior at compilation-time.
    Decorator vs Inheritance

    View Slide

  15. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 16
    Scenario
    CompanionHelper
    CompanionTroll
    Companion

    View Slide

  16. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 17
    Scenario
    CompanionHelper
    CompanionTroll
    Companion
    CompanionTrollHelper

    View Slide

  17. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 18
    Scenario
    CompanionHelper
    CompanionTroll
    Companion
    CompanionTrollHelper

    View Slide

  18. jgs
    Test Yourselves
    Connecting all

    View Slide

  19. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 35
    Assignment 03

    View Slide

  20. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 36
    Assignment 03

    View Slide

  21. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 37
    Assignment 03 - UI
    Main
    JFrame ActionListener
    PlotPanel
    JButton
    Run

    View Slide

  22. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 38
    Assignment 03 – Observer Pattern
    Main
    Observable
    PlotPanel
    Run
    Source
    Observer

    View Slide

  23. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 39
    Assignment 03 – Decorator Pattern
    Main
    PlotPanel
    Observer
    Worker
    WorkerStandard
    Tool
    ToolLine ToolSquare

    View Slide

  24. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 40
    Assignment 03 – Decorator Pattern
    ToolBar
    ToolSquare
    WorkerStandard
    ToolSquare
    WorkerStandar
    WorkerStandar

    View Slide

  25. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 41
    Assignment 03 – Singleton Pattern
    PlotPanel
    Run
    << Singleton >>
    Configurator

    View Slide

  26. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 42
    Source

    View Slide

  27. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 43
    World (1/2)

    View Slide

  28. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 44
    World (2/2)

    View Slide

  29. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 45
    PlotPanel

    View Slide

  30. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 46
    WorkerStandard

    View Slide

  31. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 47
    WorkerTool

    View Slide

  32. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 48
    WorkerToolLine

    View Slide

  33. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 49
    ToolSquare

    View Slide

  34. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 50
    Questions

    View Slide

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

    View Slide

  36. jgs

    View Slide

  37. jgs
    CSC 307
    Introduction to Software Engineering
    Lab 12:
    Complete Assignment 02
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  38. jgs
    Part A
    Requirements

    View Slide

  39. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 55
    Let’s Work
    Let’s review Assignment 01

    View Slide

  40. jgs
    Part B
    Design Patterns

    View Slide

  41. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 57
    Project – User selects Cluster and clicks Run


    View Slide

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