Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC307 Lecture 12

CSC307 Lecture 12

Introduction to Software Engineering
Design Patterns III
(202307)

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

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

    9 class ConcreteComponent extends Component { @override public void operation() { System.out.print("ConcreteComponent-Operation()"); } } ConcreteComponent
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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()
  9. 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
  10. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 |

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

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

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

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

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

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

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

    41 Assignment 03 – Singleton Pattern PlotPanel Run << Singleton >> Configurator
  18. 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]
  19. jgs

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

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

    57 Project – User selects Cluster and clicks Run ✅ ✅
  23. 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.