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

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

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

    14 class ConcreteComponent extends Component { @override public void operation() { System.out.print("ConcreteComponent-Operation()"); } } ConcreteComponent
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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()
  12. 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
  13. jgs Javier Gonzalez-Sanchez | CSC 308 | Winter 2023 |

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

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

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

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

    28 public class BasicCompanion implements Companion { @Override public void doSomething() { System.out.print("Hello Student, "); } } BasicCompanion
  18. 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
  19. 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
  20. 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
  21. 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]
  22. jgs

  23. 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
  24. 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.