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

CSC308 Lecture 16

CSC308 Lecture 16

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

Javier Gonzalez-Sanchez
PRO

October 30, 2022
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSC 308
    Software Engineering 1
    Lecture 16:
    Design Patterns – Decorator/Composite
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  2. jgs
    CSC 308
    Software Engineering I
    Lecture 16: Design Patterns – Decorator/Composite
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    users.csc.calpoly.edu/~javiergs | javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  3. jgs
    Previously …
    Design Patterns

    View Slide

  4. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 4
    GoF Patterns

    View Slide

  5. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 5
    Singleton

    View Slide

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

    View Slide

  7. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 7
    class Singleton{
    private static Singleton _instance;
    // Constructor is 'protected'
    protected Singleton() {}
    public static Singleton getInstance(){
    if (_instance == null){
    _instance = new Singleton();
    }
    return _instance;
    }
    }
    Singleton

    View Slide

  8. jgs
    Decorator & Composite
    Design Patterns

    View Slide

  9. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 9
    GoF Patterns

    View Slide

  10. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 10
    Decorator

    View Slide

  11. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 11
    Decorator

    View Slide

  12. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 12
    Decorator

    View Slide

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

    View Slide

  14. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 14
    class ConcreteComponent extends Component {
    @override
    public void operation() {
    System.out.print("ConcreteComponent-Operation()");
    }
    }
    ConcreteComponent

    View Slide

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

    View Slide

  16. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 16
    class ConcreteDecoratorA extends Decorator {
    @override
    public void operation() {
    super.operation();
    System.out.println("ConcreteDecoratorA-Operation()”);
    }
    }
    ConcreteDecoratorA

    View Slide

  17. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 17
    class ConcreteDecoratorB extends Decorator {
    @override
    public void operation() {
    super.operation();
    addedBehavior();
    System.out.println("ConcreteDecoratorB-Operation()");
    }
    void addedBehavior() {
    }
    }
    ConcreteDecoratorB

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  24. jgs
    Example 1
    Text Mode

    View Slide

  25. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 25
    Decorator

    View Slide

  26. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 26
    Main

    View Slide

  27. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 27
    public interface Companion {
    public void doSomething();
    }
    Companion

    View Slide

  28. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 28
    public class BasicCompanion implements Companion {
    @Override
    public void doSomething() {
    System.out.print("Hello Student, ");
    }
    }
    BasicCompanion

    View Slide

  29. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 29
    public class CompanionDecorator implements Companion {
    protected Companion c;
    public void add(Companion c){
    this.c = c;
    }
    @Override
    public void doSomething() {
    this.c.doSomething();
    }
    }
    CompanionDecorator

    View Slide

  30. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 30
    public class HelperCompanion extends CompanionDecorator {
    @Override
    public void doSomething(){
    super.doSomething();
    System.out.print(" I am here to help you. ");
    }
    }
    HelperCompanion

    View Slide

  31. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 31
    public class AffectiveCompanion extends CompanionDecorator {
    @Override
    public void doSomething(){
    super.doSomething();
    System.out.print(" I am here to cheer you.");
    }
    }
    AffectiveCompanion

    View Slide

  32. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 32
    Main

    View Slide

  33. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 33
    Questions

    View Slide

  34. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 34
    Office Hours
    Tuesday and Thursday 3 - 5 pm
    But an appointment required
    Sent me an email – [email protected]

    View Slide

  35. jgs

    View Slide

  36. jgs
    CSC 308
    Software Engineering 1
    Lab 16:
    Design Patterns
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  37. jgs
    Test Yourselves
    Class Diagram

    View Slide

  38. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 38
    Scenario

    View Slide

  39. jgs
    Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 | 39
    Let’s Work

    View Slide

  40. jgs
    CSC 308 Software Engineering I
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Winter 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.

    View Slide