Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

jgs Previously … Design Patterns

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

jgs Decorator & Composite Design Patterns

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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()

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

jgs Example 1 Text Mode

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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]

Slide 35

Slide 35 text

jgs

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

jgs Test Yourselves Class Diagram

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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.