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

CSC308 Lecture 13

CSC308 Lecture 13

Software Engineering I
Design Patterns - Decorator/Composite
(202210)

Javier Gonzalez-Sanchez
PRO

October 19, 2022
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs CSC 308 Software Engineering I Lecture 13: Design Patterns

    – Decorator/Composite Dr. Javier Gonzalez-Sanchez [email protected] users.csc.calpoly.edu/~javiergs | javiergs.com Building 14 -227 Office Hours: By appointment
  2. jgs Fall 2022 | 00000010 GoF Patterns

  3. jgs Fall 2022 | 00000010 Decorator

  4. jgs Fall 2022 | 00000010 Decorator vs Inheritance § 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.
  5. jgs Fall 2022 | 00000010 Decorator

  6. jgs Fall 2022 | 00000010 Decorator

  7. jgs Fall 2022 | 00000010 Component abstract class Component {

    public abstract void operation(); }
  8. jgs Fall 2022 | 00000010 ConcreteComponent class ConcreteComponent extends Component

    { @override public void operation() { System.out.print("ConcreteComponent-Operation()"); } }
  9. jgs Fall 2022 | 00000010 Decorator 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(); } } }
  10. jgs Fall 2022 | 00000010 ConcreteDecoratorA class ConcreteDecoratorA extends Decorator

    { @override public void operation() { super.operation(); System.out.println("ConcreteDecoratorA-Operation()”); } }
  11. jgs Fall 2022 | 00000010 ConcreteDecoratorB class ConcreteDecoratorB extends Decorator

    { @override public void operation() { super.operation(); addedBehavior(); System.out.println("ConcreteDecoratorB-Operation()"); } void addedBehavior() { } }
  12. jgs Fall 2022 | 00000010 Main 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(); } }
  13. jgs Fall 2022 | 00000010 Main 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(); } } ConcreteComponent - Operation() ConcreteDecoratorA - Operation() ConcreteDecoratorB - Operation()
  14. jgs Fall 2022 | 00000010 Scenario CompanionHelper CompanionTroll Companion

  15. jgs Fall 2022 | 00000010 Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper

  16. jgs Fall 2022 | 00000010 Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper

  17. jgs Example 1 Text Mode

  18. jgs Fall 2022 | 00000010 Decorator

  19. jgs Fall 2022 | 00000010 Main

  20. jgs Fall 2022 | 00000010 Companion public interface Companion {

    public void doSomething(); }
  21. jgs Fall 2022 | 00000010 BasicCompanion public class BasicCompanion implements

    Companion { @Override public void doSomething() { System.out.print("Hello Student, "); } }
  22. jgs Fall 2022 | 00000010 CompanionDecorator public class CompanionDecorator implements

    Companion { protected Companion c; public void add(Companion c){ this.c = c; } @Override public void doSomething() { this.c.doSomething(); } }
  23. jgs Fall 2022 | 00000010 HelperCompanion public class HelperCompanion extends

    CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to help you. "); } }
  24. jgs Fall 2022 | 00000010 AffectiveCompanion public class AffectiveCompanion extends

    CompanionDecorator { @Override public void doSomething(){ super.doSomething(); System.out.print(" I am here to cheer you."); } }
  25. jgs Fall 2022 | 00000010 Main

  26. jgs Fall 2022 | 00000010 Questions

  27. jgs

  28. jgs CSC 308 Software Engineering I Lab 12: Patterns Dr.

    Javier Gonzalez-Sanchez [email protected] users.csc.calpoly.edu/~javiergs | javiergs.com Building 14 -227 Office Hours: By appointment
  29. jgs Test Yourselves Class Diagram

  30. jgs Fall 2022 | 00000010 Scenario

  31. jgs Fall 2022 | 00000010 Let’s Work Nothing to submit

    yet
  32. jgs Fall 2022 | 00000010 Questions

  33. jgs CSC 308 Software Engineering I Javier Gonzalez-Sanchez, Ph.D. [email protected]

    Fall 2022 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.