Title: Chapter 7 Structural Patterns: Decorator
 1Chapter 7 -Structural Patterns Decorator
CIS 476/566 Software Architecture and Design 
Patterns
- Dr. Brahim Medjahed 
- brahim_at_umich.edu 
2Design Patterns in GoF The Big Picture 
 3What Are Structural Patterns?
- Structural Patterns 
- Deal with the composition of classes or objects 
- Two categories 
- Object structural patterns 
- Describe ways to assemble objects (e.g., 
 Decorator, Proxy)
- Class structural patterns 
- Use inheritance to compose classes (e.g., 
 Adapter)
4Decorator Pattern - Intent
- Attach additional responsibilities to an object 
 dynamically.
- Provide a flexible alternative to sub-classing 
 for extending functionality.
- Also Known As (AKA) 
- Wrapper
5Motivating Example BestCafé
- BestCafé coffee shop with a large beverage 
 offering
- Example inspired from Head First Design 
 Patterns by Bert Bates, Elisabeth Freeman, Eric
 Freeman, Kathy Sierra
- Offers four (4) beverages 
- House Blend 
- Dark Roast 
- Decaf 
- Espresso
6BestCafé  Initial Design 
 7Additional Requirements
- Customers can also select condiments 
- Milk 
- Soy 
- Mocha 
- Topped with 
- Whipped cream 
- Each one has its own charge
8Solution 1  Sub-classing 
 9Solution 1 - Problems
- Explosion of classes! 
- Maintenance nightmare 
- What happens when price of milk goes up? 
- Change code in all relevant subclasses 
- What happens when new topping called caramel is 
 added?
- Much more classes 
10Solution 2  Instance Variables 
 11Solution 2  Class Diagram 
 12Solution 2 - Problems
- What if the price of condiment changes? 
- Change the code? 
- Adding new condiments 
- Change the super class 
- We may have new beverages for which set of 
 condiments are not good.
- What if customer wants double mocha? 
13Solution 3  Decorate Objects
- Take a DarkRoast object 
- Decorate it with Mocha object 
- Decorate it with Whip object 
- Call the cost() 
- Rely on delegation to add condiments cost. 
14Solution 3  Decorate Objects (contd)
- We start with DarkRoast object 
- The customer wants Mocha. 
- Create Mocha and wrap it with DarkRoast
15Solution 3  Decorate Objects (contd)
- The customer wants Whipped Cream 
- Create a WhippedCream object and wrap it with 
 Mocha
16Solution 3  Decorate Objects (contd)
Total Cost 1.29
0.10
.99
0.20 
 17Decorator Pattern - Structure
Each component can be used on its own or wrapped 
by a decorator
Each decorator HAS-A (wraps) a component. It has 
an instance variable that refers to that component
Objects we are going to dynamically add behavior 
to. 
 18Decorator Pattern - Participants
- Component 
- Defines the interface for objects that can have 
 responsibilities added to them dynamically
- ConcreteComponent 
- Defines an object to which additional 
 responsibilities can be attached
- Decorator 
- Maintains a reference to a component object and 
 defines an interface that conforms to Components
 interface
- ConcreteDecorator 
- Adds responsibilities to the component
19BestCafé  UML Class Diagram 
 20Decorator Pattern - Applicability
- Add responsibilities to objects dynamically and 
 transparently
- Responsibilities that can be withdrawn 
- When extension by sub-classing is impractical 
- Explosion of subclasses 
- Class definition hidden or unavailable
21Decorator Pattern - Collaborations
- Decorator forwards requests to its Component 
 object.
- It may optionally perform additional operations 
 before and after forwarding the request
22Implementation - Example
- Implement UML class diagram in slide 17 
- Operation() in ConcreteComponent displays Im 
 concrete component
- Operation() in ConcreteDecoratorA displays Im 
 concrete Decorator A
- Operation() in ConcreteDecoratorB displays Im 
 concrete Decorator B
23Implementation  Example (contd)
-   // MainApp test application   class 
 MainApp      static void Main()          //
 Create ConcreteComponent and two Decorators
 ConcreteComponent c  new
 ConcreteComponent()      ConcreteDecoratorA d1
 new ConcreteDecoratorA()      ConcreteDecorato
 rB d2  new ConcreteDecoratorB()      // Link
 decorators       d1.SetComponent(c)      d2.Set
 Component(d1)      d2.Operation()
24Implementation  Example (contd)
-   // "Component"   abstract class 
 Component      public abstract void
 Operation()    // "ConcreteComponent"
 class ConcreteComponent  Component      pu
 blic override void Operation()          Console
 .WriteLine(Im Concrete Component()")
25Implementation  Example (contd)
- // "Decorator"   abstract class Decorator  
 Component      protected Component
 comp    public void SetComponent(Component
 comp)          this.comp  comp        pub
 lic override void Operation()          componen
 t.Operation()
26Implementation  Example (contd)
-   // "ConcreteDecoratorA"   class 
 ConcreteDecoratorA  Decorator      public
 override void Operation()          base.Operati
 on()      Console.WriteLine(Im Concrete
 Decorator A")
- // "ConcreteDecoratorB"   class 
 ConcreteDecoratorB  Decorator      public
 override void Operation()          base.Operati
 on()      Console.WriteLine(Im Concrete
 Decorator B")
27Exercise 1
- Implementation of BestCafé 
- Cost() returns the price of the corresponding 
 beverage/condiment
- Prices hard-coded in the methods 
- Main application 
- Create a Dark Roast decorated with double mocha 
 then Whipped Cream
28Exercise 1 - Implementation
  29Exercise 2
- Window abstract class 
- Has one abstract method draw() 
- SimpleWindow is a subclass of Window 
- draw() displays Simple Window 
- Two possible concrete decorators 
- VerticalScrollBarDecorator 
- draw() displays with vertical scroll bar 
- HorizontalScrollBarDecorator 
- draw() displays with horizontal scroll bar 
- Main Application 
- simple window with horizontal scroll bar with 
 vertical scroll bar
30Exercise 2  UML Class Diagram
  31Exercise 2  Implementation