Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

jgs 00000001 GoF Patterns

Slide 4

Slide 4 text

jgs 00000001 Decorator

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

jgs 00000001 Decorator

Slide 7

Slide 7 text

jgs 00000001 Decorator

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

jgs 00000001 Scenario CompanionHelper CompanionTroll Companion

Slide 16

Slide 16 text

jgs 00000001 Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper

Slide 17

Slide 17 text

jgs 00000001 Scenario CompanionHelper CompanionTroll Companion CompanionTrollHelper

Slide 18

Slide 18 text

jgs Example 1 Text Mode

Slide 19

Slide 19 text

jgs 00000001 Decorator

Slide 20

Slide 20 text

jgs 00000001 Main

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

jgs 00000001 Main

Slide 27

Slide 27 text

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.