CSE460 Lecture 18

CSE460 Lecture 18

Software Analysis and Design
Observer
(202010)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

July 18, 2020
Tweet

Transcript

  1. jgs CSE 460 Software Analysis and Design Lecture 18: Structural

    Patterns - Decorator Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 2 jgs

    Announcements § Midterm Exam: Monday October 19 § Midterm Review: Friday October 16
  3. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 3 jgs

    GoF Patterns
  4. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 4 jgs

    Decorator
  5. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 5 jgs

    Decorator
  6. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 6 jgs

    Decorator
  7. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 7 jgs

    Component abstract class Component { public abstract void operation(); }
  8. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 8 jgs

    ConcreteComponent class ConcreteComponent extends Component { @override public void operation() { System.out.print("ConcreteComponent-Operation()"); } }
  9. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 9 jgs

    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. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 10 jgs

    ConcreteDecoratorA class ConcreteDecoratorA extends Decorator { @override public void operation() { super.operation(); System.out.println("ConcreteDecoratorA-Operation()”); } }
  11. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 11 jgs

    ConcreteDecoratorB class ConcreteDecoratorB extends Decorator { @override public void operation() { super.operation(); addedBehavior(); System.out.println("ConcreteDecoratorB-Operation()"); } void addedBehavior() { } }
  12. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 12 jgs

    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. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 13 jgs

    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. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 14 jgs

    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.
  15. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 15 jgs

    Scenario CompanionHelper CompanionTroll Companion
  16. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 16 jgs

    Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper
  17. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 17 jgs

    Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper
  18. jgs Example 1 Text Mode

  19. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 19 jgs

    Decorator
  20. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 20 jgs

    Main
  21. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 21 jgs

    Companion public interface Companion { public void doSomething(); }
  22. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 22 jgs

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

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

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

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

    Main
  27. jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez javiergs@asu.edu

    Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE460 at ASU. They cannot be distributed or used for another purpose.