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

CSE564 Lecture 13

CSE564 Lecture 13

Software Design
Decorator
(202110)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

September 13, 2020
Tweet

More Decks by Javier Gonzalez

Other Decks in Programming

Transcript

  1. jgs CSE 564 Software Design Lecture 13: Design Patterns -

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

  3. jgs 564 00001101 GoF Patterns

  4. jgs 564 00001101 Singleton

  5. jgs 564 00001101 Singleton class Singleton{ private static Singleton _instance;

    // Constructor is 'protected' protected Singleton() {} public static Singleton getInstance(){ if (_instance == null){ _instance = new Singleton(); } return _instance; } }
  6. jgs 564 00001101 Singleton class MainApp { public static void

    Main(String []a){ // Constructor is protected -- cannot use new Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); // Test for same instance if (s1 == s2){ // true - Objects are the same instance } } }
  7. jgs 564 00001101 Logger

  8. jgs 564 00001101 Desktop

  9. jgs Composite and Decorator

  10. jgs 564 00001101 GoF Patterns

  11. jgs 564 00001101 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.
  12. jgs 564 00001101 Decorator

  13. jgs 564 00001101 Practical Examples How Java Widgets and Containers

    work?
  14. jgs 564 00001101 Component abstract class Component { public abstract

    void operation(); }
  15. jgs 564 00001101 ConcreteComponent class ConcreteComponent extends Component { @override

    public void operation() { System.out.print("ConcreteComponent-Operation()"); } }
  16. jgs 564 00001101 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(); } } }
  17. jgs 564 00001101 ConcreteDecoratorA class ConcreteDecoratorA extends Decorator { @override

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

    public void operation() { super.operation(); addedBehavior(); System.out.println("ConcreteDecoratorB-Operation()"); } void addedBehavior() { } }
  19. jgs 564 00001101 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(); } }
  20. jgs 564 00001101 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()
  21. jgs Inheritance vs Decoration/Composition

  22. jgs 564 00001101 Scenario CompanionHelper CompanionTroll Companion

  23. jgs 564 00001101 Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper

  24. jgs 564 00001101 Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper

  25. jgs 564 00001101 Decorator WhiteBody body = new WhiteBody(); Mask

    mask = new Mask(); Helmet helmet = new Helmet(); // Link decorators mask.SetComponent(body); helmet.SetComponent(mask); helmet.Operation();
  26. jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. javiergs@asu.edu Fall

    2021 Copyright. These slides can only be used as study material for the class CSE564 at ASU. They cannot be distributed or used for another purpose.