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

CSE460 Lecture 14

CSE460 Lecture 14

Software Analysis and Design
Decorator
(202103)

Javier Gonzalez-Sanchez
PRO

July 14, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 460
    Software Analysis and Design
    Lecture 14: Structural Patterns - Decorator
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    00000001
    Announcement
    § Midterm Exam
    Wednesday March 10
    During the lecture time. No exceptions
    § Final Exam
    Wednesday April 21
    During the lecture time. No exceptions

    View Slide

  3. jgs
    00000001
    GoF Patterns

    View Slide

  4. jgs
    00000001
    Decorator

    View Slide

  5. jgs
    00000001
    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.

    View Slide

  6. jgs
    00000001
    Decorator

    View Slide

  7. jgs
    00000001
    Decorator

    View Slide

  8. jgs
    00000001
    Component
    abstract class Component {
    public abstract void operation();
    }

    View Slide

  9. jgs
    00000001
    ConcreteComponent
    class ConcreteComponent extends Component {
    @override
    public void operation() {
    System.out.print("ConcreteComponent-Operation()");
    }
    }

    View Slide

  10. jgs
    00000001
    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();
    }
    }
    }

    View Slide

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

    View Slide

  12. jgs
    00000001
    ConcreteDecoratorB
    class ConcreteDecoratorB extends Decorator {
    @override
    public void operation() {
    super.operation();
    addedBehavior();
    System.out.println("ConcreteDecoratorB-Operation()");
    }
    void addedBehavior() {
    }
    }

    View Slide

  13. jgs
    00000001
    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();
    }
    }

    View Slide

  14. jgs
    00000001
    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()

    View Slide

  15. jgs
    00000001
    Scenario
    CompanionHelper
    CompanionTroll
    Companion

    View Slide

  16. jgs
    00000001
    Scenario
    CompanionHelper
    CompanionTroll
    Companion
    CompanionTrollHelper

    View Slide

  17. jgs
    00000001
    Scenario
    CompanionHelper
    CompanionTroll
    Companion
    CompanionTrollHelper

    View Slide

  18. jgs
    Example 1
    Text Mode

    View Slide

  19. jgs
    00000001
    Decorator

    View Slide

  20. jgs
    00000001
    Main

    View Slide

  21. jgs
    00000001
    Companion
    public interface Companion {
    public void doSomething();
    }

    View Slide

  22. jgs
    00000001
    BasicCompanion
    public class BasicCompanion implements Companion {
    @Override
    public void doSomething() {
    System.out.print("Hello Student, ");
    }
    }

    View Slide

  23. jgs
    00000001
    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

  24. jgs
    00000001
    HelperCompanion
    public class HelperCompanion extends CompanionDecorator {
    @Override
    public void doSomething(){
    super.doSomething();
    System.out.print(" I am here to help you. ");
    }
    }

    View Slide

  25. jgs
    00000001
    AffectiveCompanion
    public class AffectiveCompanion extends CompanionDecorator {
    @Override
    public void doSomething(){
    super.doSomething();
    System.out.print(" I am here to cheer you.");
    }
    }

    View Slide

  26. jgs
    00000001
    Main

    View Slide

  27. jgs
    CSE 460 Software Analysis and Design
    Javier Gonzalez-Sanchez
    [email protected]
    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.

    View Slide